在sql存储过程中创建复杂的case语句

时间:2013-12-30 22:02:43

标签: sql-server stored-procedures case

我正在尝试将if语句转换为SQL case语句,并且没有太多运气让逻辑正常工作。 (我的问题是SQL case statement in a stored procedure

的更新版本
if LossStatusCode == 'R' Then 'Item has been reviewed.'  

ELseif //if  LossStatusCode != 'R'
    DateOfLoss < PolicyStartDate THEN 'Invalid Date'  
    MilesDriven > TotalMilesAllowed THEN 'Mileage exceeded'

Else // if (LossStatusCode != 'R') && Date is valid && Mileage is Valid)
    LossStatusCode != 'R' Then 'Status code is Review.'

如何将上述逻辑转换为SQL Case语句?

1 个答案:

答案 0 :(得分:2)

select case when LossStatusCode = 'R' then 'Item has been reviewed.'
            when DateOfLoss < PolicyStartDate and MilesDriven > TotalMilesAllowed then 'Invalid Date, Mileage exceeded'
            when DateOfLoss < PolicyStartDate then 'Invalid Date' 
            when MilesDriven > TotalMilesAllowed then 'Mileage exceeded'
            else 'Status code is Review.'
       end