我想在商店程序中添加if else语句但找不到任何解决方案

时间:2017-10-19 06:24:43

标签: sql sql-server stored-procedures

我想将这些条件放在我的存储过程中

if ((select PriSubmissionStatusId from Claim where ClaimId = 375 )!= 106  ) Print 'Primary'
else if((select SecSubmissionStatusId from Claim where ClaimId = 375 )= 0)print 'Primary'
else if((select SecSubmissionStatusId from Claim where ClaimId = 375 )!= 106)print 'Secoundary'
else if((select OthSubmissionStatusId from Claim where ClaimId = 375 )= 0)print 'Secoundry'
else if((select OthSubmissionStatusId from Claim where ClaimId = 375 )!= 106)print 'Other'
else print 'Primary'

作为列名构建,它们也是来自不同表的存储过程中的其他列。任何想法?

1 个答案:

答案 0 :(得分:0)

使用Case When代替if else

SELECT CASE WHEN PriSubmissionStatusId <> 106   THEN 'Primary'
            WHEN SecSubmissionStatusId = 0      THEN 'Primary'
            WHEN SecSubmissionStatusId <> 106   THEN 'Secoundary'
            WHEN OthSubmissionStatusId = 0      THEN 'Secoundry'
            WHEN OthSubmissionStatusId <> 106   THEN 'Other'
        END
FROM  Claim 
WHERE ClaimId = 375