下面是我的 WHERE 子句,其中有重复(尤其是两个 @subgroup 变量),所以我想知道是否可以做些什么来优化代码。我的代码按原样运行良好,但如果我能让它更漂亮,那就太好了。首先是一个快速的解释。
我有五个从 asp 下拉列表中填充的参数。出于这个问题的目的,我用数据填充了它们:
DECLARE
@DataCollection VARCHAR(50) = '2013/14 - Autumn 1 - Targets',
@StuYear VARCHAR(2) = '11',
@SubjectName VARCHAR(100) ='English',
@TeachingGroup VARCHAR(25) = 'Select All',
@SubGroup VARCHAR(15) = 'FSMYes',
@SubGroup2 VARCHAR(15) = 'SENS'
where 语句允许根据下拉列表中的值过滤结果集。
@DataCollection、@StuYear 和 @SubjectName 都是必需的,并且在下拉列表中具有默认值。其余的都是可选的。
@subgroup 的变量是根据下拉列表中的硬编码值填充的,因此传递了一个参数,如果该参数值与 WHERE 子句中的条件匹配,则将特定的 db 值过滤到表的特定列中。
这是我的 SQL:
WHERE
[StuYear] = @StuYear AND
[DataCollection] = @DataCollection AND
[Name] = @SubjectName AND (
@TeachingGroup = 'Select All' OR
[TeachingGroup] = @TeachingGroup
) AND (
@SubGroup = 'Select All' OR
Gender = CASE --Gender
WHEN @SubGroup = 'GenF' THEN 'F'
WHEN @SubGroup = 'GenM' THEN 'M'
END
OR
LEFT(ks2av,1) = CASE --Attainer
WHEN @SubGroup = 'High' THEN '5'
WHEN @SubGroup = 'High' THEN '5'
WHEN @SubGroup = 'Middle' THEN '4'
WHEN @SubGroup = 'Low' THEN '3'
WHEN @SubGroup = 'Low' THEN '2'
WHEN @SubGroup = 'Low' THEN '1'
END
OR
PupilPremium = CASE --Pupil Premium
WHEN @Subgroup = 'PPYes' THEN 'Yes'
END
OR
FSM = CASE --Free School Meals
WHEN @Subgroup = 'FSMYes' THEN 'Yes'
END
OR
SEN = CASE --SEN
WHEN @Subgroup = 'SENA' THEN 'A'
WHEN @Subgroup = 'SENP' THEN 'P'
WHEN @Subgroup = 'SENS' THEN 'S'
WHEN @Subgroup = 'SENNo' THEN 'N'
END
OR
(@SubGroup='SENYes' AND SEN IN ('A','P','S')) --SEN Yes
OR
EAL = CASE --English as an Additional Language
WHEN @Subgroup = 'EALYes' THEN 'Yes'
END
OR
LAC = CASE --Looked After Children
WHEN @Subgroup = 'LACYes' THEN 'Yes'
END
OR --Gifted & Talented
GandT = CASE
WHEN @Subgroup = 'GandTYes' THEN 'Yes'
END
OR --Gifted Only
Gifted = CASE
WHEN @Subgroup = 'GiftedYes' THEN 'Yes'
END
OR --Talented Only
Talented = CASE
WHEN @Subgroup = 'TalentedYes' THEN 'Yes'
END
)AND (
@subgroup2 = 'Select All' OR
Gender = CASE --Gender
WHEN @subgroup2 = 'GenF' THEN 'F'
WHEN @subgroup2 = 'GenM' THEN 'M'
END
OR
LEFT(ks2av,1) = CASE --Attainer
WHEN @subgroup2 = 'High' THEN '5'
WHEN @subgroup2 = 'High' THEN '5'
WHEN @subgroup2 = 'Middle' THEN '4'
WHEN @subgroup2 = 'Low' THEN '3'
WHEN @subgroup2 = 'Low' THEN '2'
WHEN @subgroup2 = 'Low' THEN '1'
END
OR
PupilPremium = CASE --Pupil Premium
WHEN @subgroup2 = 'PPYes' THEN 'Yes'
END
OR
FSM = CASE --Free School Meals
WHEN @subgroup2 = 'FSMYes' THEN 'Yes'
END
OR
SEN = CASE --SEN
WHEN @subgroup2 = 'SENA' THEN 'A'
WHEN @subgroup2 = 'SENP' THEN 'P'
WHEN @subgroup2 = 'SENS' THEN 'S'
WHEN @subgroup2 = 'SENNo' THEN 'N'
END
OR
(@subgroup2='SENYes' AND SEN IN ('A','P','S')) --SEN Yes
OR
EAL = CASE --English as an Additional Language
WHEN @subgroup2 = 'EALYes' THEN 'Yes'
END
OR
LAC = CASE --Looked After Children
WHEN @subgroup2 = 'LACYes' THEN 'Yes'
END
OR --Gifted & Talented
GandT = CASE
WHEN @subgroup2 = 'GandTYes' THEN 'Yes'
END
OR --Gifted Only
Gifted = CASE
WHEN @subgroup2 = 'GiftedYes' THEN 'Yes'
END
OR --Talented Only
Talented = CASE
WHEN @subgroup2 = 'TalentedYes' THEN 'Yes'
END
)