我想在SQL中编写以下IF语句。大多数where子句都是构造的,但这是我正在努力解决的语句的条件:
if @StuYear = 11 then
AND (@TeachingGroup = 'Select All')
AND ([DataCollection] = @DataCollection)
AND ([Name] = @SubjectName)
AND (@Subgroup='Select All')
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABU]%' AND Result <> ''
else if @StuYear = 10 then
AND @TeachingGroup Not Like 'Select All'
AND ([DataCollection] = @DataCollection)
AND ([Name] = @SubjectName)
AND ([TeachingGroup] = @TeachingGroup)
AND @Subgroup='Select All'
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABC]%' AND Result <> ''
end if
答案 0 :(得分:1)
只需将if
替换为where
,将else if
替换为or
,并将其他条件置于paranthesis中:
...
where (@StuYear = 11
AND (@TeachingGroup = 'Select All')
AND ([DataCollection] = @DataCollection)
AND ([Name] = @SubjectName)
AND (@Subgroup='Select All')
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'U'
AND Result Not Like '' )
OR (@StuYear = 10
AND @TeachingGroup Not Like 'Select All'
AND ([DataCollection] = @DataCollection)
AND ([Name] = @SubjectName)
AND ([TeachingGroup] = @TeachingGroup)
AND @Subgroup='Select All'
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'C'
AND Result Not Like '')
答案 1 :(得分:0)
where
(@StuYear = [stuyear]
AND @TeachingGroup = 'Select All'
AND [DataCollection] = @DataCollection
AND [Name] = @SubjectName
AND @Subgroup='Select All'
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')
OR
(@StuYear = [stuyear]
AND @TeachingGroup Not Like 'Select All'
AND [DataCollection] = @DataCollection
AND [Name] = @SubjectName
AND [TeachingGroup] = @TeachingGroup
AND @Subgroup='Select All'
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')