[PL]/DB

[MSSQL]해당년도 동안 입력된 레코드의 갯수 알아보는 스크립트

객과 함께. 2011. 11. 16. 23:47

레코드의 갯수를 가져 오는 스크립트

작업 하다가 필요해서 작성한 스크립트 입니다.

create proc TotalRowCount
 @form_id int,
 @result int output   
as
 declare @totalcnt int
 declare @nowYear datetime
 declare @strYMD datetime
begin
 
 select @totalcnt = count(*) from TB_A01_DOC where form_id = @form_id

 if (@totalcnt != 0)
  begin
   set @nowYear = convert(DateTime, substring(convert(char(10), getdate(),126), 1, 4) + '-12-31')  -- 해당 년의 마지막
   set @strYMD = convert(DateTime ,convert(char(10),getdate(),126))                                -- 현재의 일자를 얻어옴.


   if (@nowYear < @strYMD)
    begin
     set @result = 1  --현재 년의 마지막보다 클때는 1로 초기화 시킴.
    end
   else
    begin
     set @result = @totalcnt + 1   --현재 값에 1만 더함.
    end
  end
 else
  begin
   set @result = 1
  end
end
go