在SQL中如何实现类似C#的switch语句

时间:2022-09-08 16:34:56

需要做一个类似于C#switch语句,在MS SQL中可以实现如下:


SELECT ID,ParentID,[Level], Name, 'TestCase', 
COALESCE
( Case When r.Result = 2 Then 'Passed' Else null End,
Case When r.Result = 3 Then 'Failed' Else null End,
Case When r.Result = 7 Then 'Blocked' Else null End,
Case When r.Result = 255 Then 'NotRun' Else null End

FROM @TestCase c JOIN @TestResult r ON c.ID = r.CaseId 


COALESCE语句: http://msdn.microsoft.com/en-us/library/ms190349.aspx