StoredProcedure는 각종 업무규정이나 계산이나, 조인문제들

을 한꺼번에 작성해두고 필요할때 사용하기에 처리시간면이나,

업무 오류를 최소화 하는 면에서나 편리하게 사용할 수 있습니다.

하지만, storedprocedure는 다른 테이블들과 join하여 사용할

수 없습니다...

이렇게 조금은 껄끄러운 문제를 해결할 수 있는 것이 Function

이란 넘입니다.



ALTER  Function 인사_근무일보
   (@PARAM varchar(300))

returns @근무일보  Table
      (근무일자   datetime null,
       사원번호   char(8) null,
       성명       char(12) null,
       공장       char(2) null,
       공장명     char(12) null,
       부서코드   char(4) null,
       부서명     char(20) null,
       LINE       char(4) null,
       근무형태   char(2) null,
       근무형태명 char(10) null,
       출근시간   datetime null,
       퇴근시간   datetime null,
       출근시     char(2) null,
       출근분     char(2) null,
       출근시분   char(5) null,
       퇴근시     char(2) null,
       퇴근분     char(2) null,
       퇴근시분   char(5) null,
       정상근무   money null,
       잔업시간   money null,
       근태공제   money null,
       부서장결재 char(1) null,
       총무결재   char(1) null)
as
begin
declare @부서장결재 char(1),  @총무결재 char(1), @부서코드 char(4),
        @시작일자   Datetime, @종료일자 datetime, @사원번호 char(8)

select @부서코드 = PM1,
       @시작일자 = PM2,
       @종료일자 = PM3,
       @사원번호 = PM4,
       @부서장결재 = PM5,
       @총무결재 = PM6
  from PARAM_SET(@PARAM)

if   isnull(@부서코드,' ')   = ' ' set @부서코드 = '@'
if   isnull(@사원번호,' ')   = ' ' set @사원번호 = '@'
if   isnull(@부서장결재,' ') = ' ' set @부서장결재 = '@'
if   isnull(@총무결재,' ')   = ' ' set @총무결재 = '@'
if   isnull(@시작일자,'1900-01-01') <= '1900-01-01'
     select @시작일자 = min(근무일자) from 인사근무일보
if   isnull(@종료일자,'1900-01-01') <= '1900-01-01'
     select @종료일자 = max(근무일자) from 인사근무일보

insert into @근무일보
SELECT a.근무일자, a.사원번호, 성명 = M.성명, a.공장, 공장명 = substring(N1.NM,1,12),
       a.부서코드, 부서명 = S.SCDNM, a.LINE,
       a.근무형태, 근무형태명 = substring(N2.NM,1,10),a.출근시간, a.퇴근시간,
       출근시   = replace(str(Datepart(hh,a.출근시간),     2),' ','0'),
       출근분   = replace(str(Datepart(minute, a.출근시간),2),' ','0'),
       출근시분 = replace(str(Datepart(hh,a.출근시간),     2),' ','0') + ':' +
                  replace(str(Datepart(minute, a.출근시간),2),' ','0'),
       퇴근시   = replace(str(Datepart(hh,a.퇴근시간),     2),' ','0'),
       퇴근분   = replace(str(Datepart(minute, a.퇴근시간),2),' ','0'),
       퇴근시분 = replace(str(Datepart(hh,a.퇴근시간),     2),' ','0') + ':' +
                  replace(str(Datepart(minute, a.퇴근시간),2),' ','0'),
       정상근무, 잔업시간, 근태공제, 부서장결재, 총무결재
  FROM 인사근무일보 a
  left outer join 인사사원신상         M  on a.사원번호 = M.사원번호
  left outer join COD01                S  on a.부서코드 = S.SCD
  left outer join name_ref('공장')     N1 on a.공장 = N1.CD
  left outer join name_ref('근무형태') N2 on a.근무형태 = N2.CD
 where 근무일자 between @시작일자 and @종료일자
   and ((substring(@부서코드,1,1) = '@')   or (a.부서코드   = @부서코드))
   and ((substring(@사원번호,1,1) = '@')   or (a.사원번호   = @사원번호))
   and ((substring(@총무결재,1,1) = '@')   or (a.총무결재   = @총무결재))
   and ((substring(@부서장결재,1,1) = '@') or (a.부서장결재 = @부서장결재))

return
end

+ Recent posts