如何在变量中放置一个语句并在存储过程中多次使用它

时间:2014-04-18 07:22:06

标签: sql-server stored-procedures

我创建了一个存储过程,其中一个case语句将被使用5次,所以有没有办法创建一个SQL变量,所以我可以多次使用该变量? 我创建了一个存储过程,其中一个case语句将被使用5次,所以有没有办法创建一个SQL变量,所以我可以多次使用该变量?

@ Designation_Level1 varchar(100)= null, @pk_Designation_ID varchar(100)= null

AS
BEGIN
    声明@JoiningDateChkforRule date = null     声明@MPHILDateChkforRule date = null     声明@caseVar varchar(max)
    设置@ caseVar =''     SET @JoiningDateChkforRule ='1991-09-19 00:0:00.000'     SET @MPHILDateChkforRule ='1993-12-31 00:0:00.000'

set @Sql='SELECT Distinct SR.pk_employee_ID ,ISNULL(Last_Name,'''')+ '' ''+ISNULL(First_Name,'''')+'' ''+ISNULL(Middle_Name,'''') AS EmpName,Emp_Code,
TJoiningDateforRuleDesg.JoiningDateforRuleDesg,
-- if main designation and rule designation are same
--Main designation
CASE '+@pk_Designation_ID+' 
WHEN ' +cast(@pk_Designation_IDForRule as varchar) +' THEN 
--******************    
CASE    WHEN DATEDIFF(DAY, TJoiningDateforRuleDesg.JoiningDateforRuleDesg, '+cast(@JoiningDateChkforRule as varchar)+') > 0
    THEN TJoiningDateforRuleDesg.JoiningDateforRuleDesg
    ELSE 
        CASE    WHEN DATEDIFF(day, isnull(NETSETQual.NETSETPassing_Date,MPHILQual.MPHILQualPassing_Date), isnull(MPHILQual.MPHILQualPassing_Date,NETSETQual.NETSETPassing_Date)) >= 0
                THEN 
                    CASE    WHEN DATEDIFF(day, NETSETQual.NETSETPassing_Date, TJoiningDateforRuleDesg.JoiningDateforRuleDesg) <= 0
                            THEN NETSETQual.NETSETPassing_Date
                            ELSE 
                                CASE    WHEN NETSETQual.NETSETPassing_Date IS NULL
                                        THEN 
                                            CASE    WHEN DATEDIFF(DAY, MPHILQual.MPHILQualPassing_Date,'+CAST(@MPHILDateChkforRule as varchar) +')<= 0
                                                    THEN  MPHILQual.MPHILQualPassing_Date 
                                                    ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                                            END
                                        ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                                END
                    END
                ELSE 
                    CASE    WHEN DATEDIFF(day, MPHILQual.MPHILQualPassing_Date, TJoiningDateforRuleDesg.JoiningDateforRuleDesg) <= 0
                            THEN 
                                CASE    WHEN DATEDIFF(DAY, MPHILQual.MPHILQualPassing_Date,'+CAST(@MPHILDateChkforRule as varchar) +') <= 0
                                        THEN  MPHILQual.MPHILQualPassing_Date 
                                        ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                                END
                            ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                    END                     
        END
END         
--******************                            
ELSE -- if main designation and rule designation are different
(Select min(Joining_Date) from HRMS_Employee_ServiceRecord  (nolock) 
where fk_Designation_ID in (SELECT * from dbo.Split('''+@pk_Designation_ID+''','','')) 
and  pk_Employee_ID = SR.pk_Employee_ID) 
END as Senority_Joining_For_Order,  
-- Level_1_Designation
CASE '+@Designation_Level1+'
WHEN '+cast(@pk_Designation_IDForRule as varchar) +' THEN 
--******************    
CASE    WHEN DATEDIFF(DAY, TJoiningDateforRuleDesg.JoiningDateforRuleDesg, '+cast(@JoiningDateChkforRule as varchar)+') > 0
    THEN TJoiningDateforRuleDesg.JoiningDateforRuleDesg
    ELSE 
        CASE    WHEN DATEDIFF(day, isnull(NETSETQual.NETSETPassing_Date,MPHILQual.MPHILQualPassing_Date), isnull(MPHILQual.MPHILQualPassing_Date,NETSETQual.NETSETPassing_Date)) >= 0
                THEN 
                    CASE    WHEN DATEDIFF(day, NETSETQual.NETSETPassing_Date, TJoiningDateforRuleDesg.JoiningDateforRuleDesg) <= 0
                            THEN NETSETQual.NETSETPassing_Date
                            ELSE 
                                CASE    WHEN NETSETQual.NETSETPassing_Date IS NULL
                                        THEN 
                                            CASE    WHEN DATEDIFF(DAY, MPHILQual.MPHILQualPassing_Date,'+CAST(@MPHILDateChkforRule as varchar) +')<= 0
                                                    THEN  MPHILQual.MPHILQualPassing_Date 
                                                    ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                                            END
                                        ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                                END
                    END
                ELSE 
                    CASE    WHEN DATEDIFF(day, MPHILQual.MPHILQualPassing_Date, TJoiningDateforRuleDesg.JoiningDateforRuleDesg) <= 0
                            THEN 
                                CASE    WHEN DATEDIFF(DAY, MPHILQual.MPHILQualPassing_Date,'+CAST(@MPHILDateChkforRule as varchar) +') <= 0
                                        THEN  MPHILQual.MPHILQualPassing_Date 
                                        ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                                END
                            ELSE TJoiningDateforRuleDesg.JoiningDateforRuleDesg 
                    END                     
        END
END     

--******************                                
ELSE
(Select min(Joining_Date) from HRMS_Employee_ServiceRecord  (nolock) 
where fk_Designation_ID in (SELECT * from dbo.Split('''+@Designation_Level1+''','','')) 
and  pk_Employee_ID = SR.pk_Employee_ID) 
END as Level_1_Designation
FROM HRMS_EMPLOYEE_SERVICERECORD SR(NOLOCK)
INNER JOIN HRMS_Mst_Employee MstEmp (NOLOCK) ON SR.pk_Employee_ID=MstEmp.pk_Employee_ID AND Sr.IsCurrent_Appointment=''1'' AND Sr.fk_SeparationType_ID is null   AND Sr.Is_Approved=''1''   

INNER JOIN 
(
Select pk_Employee_ID, min(Joining_Date) as JoiningDateforRuleDesg  from HRMS_Employee_ServiceRecord  (nolock) 
where fk_Designation_ID = '+CAST(@pk_Designation_IDForRule as varchar)+'
group by pk_Employee_ID
)TJoiningDateforRuleDesg ON TJoiningDateforRuleDesg.pk_Employee_ID = SR.pk_Employee_ID

LEFT JOIN 
(
Select Distinct RuleEQ.pk_Employee_ID as NETSETpk_Employee_ID, min(RuleEQ.Passing_Date) as NETSETPassing_Date
FROM HRMS_Employee_Qualifications RuleEQ        
WHERE
fk_Course_ID in  (SELECT * from dbo.Split('''+(@NETSETQualification)+''','','')) 
group by RuleEQ.pk_Employee_ID
)NETSETQual ON
NETSETQual.NETSETpk_Employee_ID = SR.pk_Employee_ID

LEFT JOIN 
(
Select Distinct RuleEQ.pk_Employee_ID as MPHILQualpk_Employee_ID, min(RuleEQ.Passing_Date) as MPHILQualPassing_Date
FROM HRMS_Employee_Qualifications RuleEQ        
WHERE 
fk_Course_ID in   (SELECT * from dbo.Split('''+(@MPHILQualification)+''','','')) 
group by RuleEQ.pk_Employee_ID
)MPHILQual ON 
MPHILQual.MPHILQualpk_Employee_ID = SR.pk_Employee_ID   
LEFT JOIN HRMS_Employee_Qualifications RuleEQ ON RuleEQ.pk_Employee_ID = SR.pk_employee_ID
where 
MstEmp.deleted=''0'' and 
SR.Appointment_Type in (''3'' , ''2'') 
and SR.fk_SeparationType_ID is null and
SR.fk_Designation_ID='+ cast(@pk_Designation_IDForRule as varchar)+' AND        
( (TJoiningDateforRuleDesg.JoiningDateforRuleDesg < '+cast(@JoiningDateChkforRule as varchar)+')
        OR
(TJoiningDateforRuleDesg.JoiningDateforRuleDesg >= '+cast(@JoiningDateChkforRule as varchar)+' AND RuleEQ.fk_Course_ID IN (SELECT * from dbo.Split('''+@NETSETQualification+''','',''))) 
        OR
(TJoiningDateforRuleDesg.JoiningDateforRuleDesg >= '+cast(@JoiningDateChkforRule as varchar)+' AND RuleEQ.fk_Course_ID IN (SELECT * from dbo.Split('''+@MPHILQualification+''','','')) )    ) '
-- and ES.pk_CourseBranch_ID in (35) 

end

1 个答案:

答案 0 :(得分:1)

不,你不能把它放到变量中,除非你将全部使用动态SQL。

根据代码的实际外观,您可以使用视图(或函数)或CTE来封装它的一部分。

我宁愿留下五次写作,期待更好的表现。