从存储过程转换为SQL查询

时间:2015-08-17 08:45:39

标签: sql-server sql-server-2008 stored-procedures

我想要第三个过程的SQL查询

将此存储过程转换为SQL查询:

CREATE PROC [WaterlilyT].[uisp_Rep_HRClearance_RND]
(
        @pnCompCode DECIMAL(18,0)=0,
        @pnClearanceNo VARCHAR(5000)='',
        @AsonDate varchar(20)='1900-01-01'
)
AS 
BEGIN
    SET NOCOUNT ON
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    IF OBJECT_ID('tempdb..#TempHRCLNos') IS NOT NULL 
       DROP TABLE #TempHRCLNos

    SELECT CAST(val AS Decimal(18,0)) clslno 
    INTO #TempHRCLNos  
    FROM dbo.split(@pnClearanceNo,',')   

    CREATE NONCLUSTERED INDEX ix_TempHRCLslno ON #TempHRCLNos(clslno) 

    DECLARE @pnVisaReq decimal
    SET @pnVisaReq = 0

    DECLARE @ldAsonDate Date
    SET @ldAsonDate = CONVERT(DATETIME, AsonDate, 103)

        --if OBJECT_ID('tempdb..#TempHRCLVisaReq') is not null drop table #TempHRCLVisaReq
        if OBJECT_ID('tempdb..#TempHRCLJoining') is not null drop table #TempHRCLJoining

        SELECT c.serialno,d.posiref posname,d.gender,count(a.serialno) visareq,count(b.serialno) visarefused,count(e.serialno) visarec
        Into #TempHRCLVisaReq
        FROM [WaterlilyT].[vtwfhclearance] c 
        Join [WaterlilyT].[vtwfhvisaprocreq] a on a.clearno = c.serialno
        join waterlilyt.vtwfhofferletter ofr on ofr.serialno = a.offrefno 
        join waterlilyt.vtwfhresbnk res on res.serialno = ofr.candcode 
        join [WaterlilyT].[vtwfdpclearance] d on d.serialno = c.serialno and d.gender = res.gender 
        left join [WaterlilyT].[vtwfhvisaprociss] b on b.visareq = a.serialno and b.visastat = 20
        left join [WaterlilyT].[vtwfhvisaprociss] e on e.visareq = a.serialno and e.visastat = 10
        where  isnull(a.appstat,0) = 50 
        and  (@pnCompCode =0 or (@pnCompCode !=0  and @pnCompCode = a.bcd))
        and (@pnClearanceNo ='' or (@pnClearanceNo != '' and exists (select 1 from  #TempHRCLNos where clslno=a.clearno)))
        and (@ldAsonDate = '1900-01-01' or (@ldAsonDate <> '1900-01-01' and c.clearpdt < Dateadd(day,1,@ldAsonDate)  ) )
        Group by c.serialno,d.posiref,d.gender


        SELECT c.serialno,e.codeval desg,f.gender gendercode ,count(jp.serialno) joincnt
        Into #TempHRCLJoining
        FROM WaterlilyT.vtwfhjoinprocess jp 
        join waterlilyt.vtwfhresbnk f on f.serialno = jp.candcd 
        Join [WaterlilyT].[vtwfhvisaprocreq] vreq on jp.offerno = isnull(vreq.offrefno,0) 
        Join [WaterlilyT].[vtwfhclearance] c on vreq.clearno = c.serialno 
        --Join UlexM.vmunmglobal e on isnull(e.serialno,'') = vreq.posname and  gtype = 'DESIG' and isactive = 1 and islstlvl = 1 
        --Join [WaterlilyT].[vtwfdpclearance] d on d.serialno = c.serialno and d.posiref = isnull(e.strvalue,0) and d.gender = f.gender
        join Ulexm.vmundgeneral e on e.mastcd = 'CLRPOS' and e.codeval = vreq.posname
        Join [WaterlilyT].[vtwfdpclearance] d on d.serialno = c.serialno and d.posiref = isnull(e.codeval,0) and d.gender = f.gender
        where  isnull(jp.appstat,0) =50 and jp.jointyp <> 10 and isnull(vreq.appstat,0) = 50  
        and  (@pnCompCode =0 or (@pnCompCode !=0  and @pnCompCode = jp.bcd))
        and (@pnClearanceNo ='' or (@pnClearanceNo != '' and exists (select 1 from  #TempHRCLNos where clslno=vreq.clearno)))
        and (@ldAsonDate = '1900-01-01' or (@ldAsonDate <> '1900-01-01' and c.clearpdt < Dateadd(day,1,@ldAsonDate)) )
        Group by c.serialno,e.codeval,f.gender

        /*SELECT hc.serialno,SlNo,ClearanceNo,Convert(Varchar(10),Clearancedate,103) Clearancedate,Positioncode,PositionName,
        hc.Gender,isnull(TotalNumber,0) TotalNumber,validityDate,
        Case When validityDate >= Case when @ldAsonDate ='1900-01-01' then getdate()  else @ldAsonDate end  then  
        DATEDIFF(DAY,Case when @ldAsonDate ='1900-01-01' then getdate() else @ldAsonDate end,validityDate) Else  0 End DaysToGo,
        --isnull(vreq.visareq,0) [VisaRequested],
        isnull(req.visareq,0) [VisaRequested],
        isnull(req.visarefused,0)  RefusedByROP ,isnull(req.visareq,0)-isnull(req.visarefused,0)  [VisaRecd],isnull(jp.joincnt,0) Joined,
        (isnull(req.visareq,0)-isnull(req.visarefused,0)) - isnull(jp.joincnt,0) EmpYetToJoin,VisaExpired,
        isnull(TotalNumber,0) - (isnull(req.visareq,0) + isnull(req.visarefused,0) + VisaExpired ) Pending 
        FROM WaterlilyT.vinforClearance hc
        --left Join #TempHRCLVisaReq vreq on hc.serialno =vreq.serialno and hc.desg = vreq.desg
        --Left Join #TempHRCLJoining jp on hc.serialno =jp.serialno and hc.desg =jp.desg
        Left Join #TempHRCLJoining jp on hc.serialno =jp.serialno and hc.PositionCode =jp.desg and jp.gendercode = hc.gendercode 
        Left Join #TempHRCLVisaReq req on req.serialno = hc.serialno and req.posname = hc.PositionCode and req.gender = hc.gendercode 
        where  (@pnCompCode =0 or (@pnCompCode !=0  and @pnCompCode = hc.bcd))
        and (@pnClearanceNo ='' or (@pnClearanceNo != '' and exists (select 1 from  #TempHRCLNos where clslno=hc.serialno)))
        and (@ldAsonDate = '1900-01-01' or (@ldAsonDate <> '1900-01-01' and Clearancedate < Dateadd(day,1,@ldAsonDate)  ) )*/

        SELECT hc.serialno,SlNo,ClearanceNo,Convert(Varchar(10),Clearancedate,103) Clearancedate,Positioncode,PositionName,
        hc.Gender,isnull(TotalNumber,0) TotalNumber,validityDate,
        Case When validityDate >= Case when @ldAsonDate ='1900-01-01' then getdate()  else @ldAsonDate end  then  
        DATEDIFF(DAY,Case when @ldAsonDate ='1900-01-01' then getdate() else @ldAsonDate end,validityDate) Else  0 End DaysToGo,
        isnull(req.visareq,0) [VisaRequested],
        isnull(req.visarefused,0)  RefusedByROP ,isnull(req.visarec,0)  [VisaRecd],isnull(jp.joincnt,0) Joined,
        isnull(req.visarec,0) - isnull(jp.joincnt,0) EmpYetToJoin,VisaExpired,
        isnull(TotalNumber,0) - (isnull(req.visareq,0) + isnull(req.visarefused,0) + VisaExpired ) Pending 
        FROM WaterlilyT.vinforClearance hc
        --left Join #TempHRCLVisaReq vreq on hc.serialno =vreq.serialno and hc.desg = vreq.desg
        --Left Join #TempHRCLJoining jp on hc.serialno =jp.serialno and hc.desg =jp.desg
        Left Join #TempHRCLJoining jp on hc.serialno =jp.serialno and hc.PositionCode =jp.desg and jp.gendercode = hc.gendercode 
        Left Join #TempHRCLVisaReq req on req.serialno = hc.serialno and req.posname = hc.PositionCode and req.gender = hc.gendercode 
        where  (@pnCompCode =0 or (@pnCompCode !=0  and @pnCompCode = hc.bcd))
        and (@pnClearanceNo ='' or (@pnClearanceNo != '' and exists (select 1 from  #TempHRCLNos where clslno=hc.serialno)))
        and (@ldAsonDate = '1900-01-01' or (@ldAsonDate <> '1900-01-01' and Clearancedate < Dateadd(day,1,@ldAsonDate)  ) )

        END

0 个答案:

没有答案