如何在sql变量中放置一个特定的case语句,并在我使用动态查询的存储过程中重用它?请帮帮我
@Designation_Level1 varchar(100) = null,
@pk_Designation_ID varchar(100) = null
AS
BEGIN
declare @JoiningDateChkforRule date=null
declare @MPHILDateChkforRule date=null
declare @caseVar varchar(max)
set @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
// the --**** block i am going to use for multiple times