我目前有一条select语句,它会根据条件而变化:
if @LoadUsage = '|CreateBatch|'
begin
-- Do lookup on ClaimNumber and InterchangeName provided
-- Use @version scalar to filter latest InputAdjustmentVersion
select @ClaimSubmissionNumber = CS.ClaimSubmissionNumber
from ClaimSubmissions CS inner join Claims C on C.ClaimID = CS.ClaimID
inner join InterchangeInfo I on CS.InterchangeId = I.InterchangeId
where I.InterchangeName = @InterchangeName
and C.ClaimNumber = @ClaimNumber
and CS.SubmissionStatus in (select SubmissionStatus from @AllowedSubmissionStatus)
and Cs.InputAdjustmentVersion = @Version
end
else
begin
-- Do lookup on ClaimNumber and InterchangeName provided
select @ClaimSubmissionNumber = CS.ClaimSubmissionNumber
from ClaimSubmissions CS inner join Claims C on C.ClaimID = CS.ClaimID
inner join InterchangeInfo I on CS.InterchangeId = I.InterchangeId
where I.InterchangeName = @InterchangeName
and C.ClaimNumber = @ClaimNumber
and CS.SubmissionStatus in (select SubmissionStatus from @AllowedSubmissionStatus)
end
如果@LoadUsage
标量为'|CREATEBATCH|'
,则向where子句添加另一个检查(and Cs.InputAdjustmentVersion = @Version
)。有没有办法用where子句中的case语句来代替两个不同的select语句呢?
答案 0 :(得分:1)
只需使用OR
:
select @ClaimSubmissionNumber = CS.ClaimSubmissionNumber
from ClaimSubmissions CS inner join Claims C on C.ClaimID = CS.ClaimID
inner join InterchangeInfo I on CS.InterchangeId = I.InterchangeId
where I.InterchangeName = @InterchangeName
and C.ClaimNumber = @ClaimNumber
and CS.SubmissionStatus in (select SubmissionStatus from @AllowedSubmissionStatus)
and (@LoadUsage <> '|CreateBatch|' or Cs.InputAdjustmentVersion = @Version)
答案 1 :(得分:1)
是的,您应该能够添加@LoadUsage ='| CreateBatch |'到WHERE,并根据true或false确定适当的逻辑。例如
WHERE @LoadUsage = '|CreateBatch|' AND (..logic 1....)
OR @LoadUsage <> '|CreateBatch|' AND (...logic 2...)