question

clairaustin avatar image
clairaustin asked

Case expressions may only be nested to level 10

Below is my sql so far: update Claims set Claims. CustomText94 = ( case when ( Claims . CoverageID = 10 and c13.CodeID =10220 and claims.TypeOfClaimID is NULL) then (x . DisplayCode+'-'+c13.Description) else ( Case when (Claims.CoverageID = 10 and C13.CodeID = 10220 and t.DisplayCode = 'IO') then (x.DisplayCode+'-'+C13.Description+'-'+t.DisplayCode) else ( case when (Claims.CoverageID = 10 and c13.CodeID = 10223 and claims.TypeOfClaimID is NULL) then (x.DisplayCode+'-'+c13.Description) else ( case when (Claims.CoverageID = 10 and C13.CodeID = 10224 and claims.TypeOfClaimID Is NULL) then (x.DisplayCode+'-'+c13.Description) else ( case when (claims.CoverageID = 10 and claims.CustomCode13ID IS NULL and t.DisplayCode = 'IO') then (x.DisplayCode+'-'+t.Description) else (case when (claims.CoverageID = 10 and c13.CodeID = 10225 and claims.TypeOfClaimID is NULL) then (x.DisplayCode+'-'+c13.Description) else (case when (claims.CoverageID = 10 and c13.CodeID = 10225 and t.DisplayCode = 'IO') then (x.DisplayCode+'-'+c13.Description+'-'+t.DisplayCode) else (case when (claims.CoverageID = 10 and c13.CodeID = 10227) then (x.DisplayCode+'-'+c13.Description) else (case when (claims.CoverageID = 10 and c13.CodeID = 10228) then (x.DisplayCode+'-'+c13.Description) else (case when (x.CoverageID = 10 and c12.CodeID = 10241 and c13.CodeID = 10225 and claims.TypeOfClaimID is NULL) then (x.DisplayCode+'-'+c12.Description+'-'+c13.DisplayCode) else (case when (x.CoverageID = 10 and c12.CodeID = 10241 and c13.CodeID = 10225 and t.DisplayCode='IO') then (x.DisplayCode+'-'+c12.Description+'-'+c13.DisplayCode+'-'+t.DisplayCode) end)end)end)end)end)end)end)end)end)end)end) from Claims left join codes c12 on c12 . codeid = claims . customcode12id left join codes c13 on c13 . CodeID = claims . customcode13id left join TypeOfClaims t on t . TypeOfClaimID= Claims .TypeOfClaimID inner join Coverages x on x .CoverageID = Claims.CoverageID
case-statement
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

KenJ avatar image KenJ commented ·
That is correct - http://msdn.microsoft.com/en-us/library/ms181765.aspx Did you have a question about nesting CASE statements or were you just sharing that detail from Books Online?
0 Likes 0 ·
clairaustin avatar image clairaustin commented ·
I have a question. I am getting an error because I have over 10 nested case statements. Is there a way to streamline this?
0 Likes 0 ·

1 Answer

·
ThomasRushton avatar image
ThomasRushton answered
It looks to me as though you're getting a bit confused about the structure of the [`CASE`][1] statement, and have basically written: SELECT CASE WHEN x THEN ... ELSE CASE WHEN x THEN ... building up your big nested CASE scenario. What you could do to simplify your code, and work within the normal `CASE` statement structure is this: SELECT foo = CASE WHEN x1 THEN y1 WHEN x2 THEN y2 ... ELSE z -- use this when none of the others apply END So. In your specific case, try this: UPDATE Claims SET Claims.CustomText94 = CASE WHEN ( Claims.CoverageID = 10 AND c13.CodeID = 10220 AND claims.TypeOfClaimID IS NULL ) THEN ( x.DisplayCode + '-' + c13.Description ) WHEN ( Claims.CoverageID = 10 AND C13.CodeID = 10220 AND t.DisplayCode = 'IO' ) THEN ( x.DisplayCode + '-' + C13.Description + '-' + t.DisplayCode ) WHEN ( Claims.CoverageID = 10 AND c13.CodeID = 10223 AND claims.TypeOfClaimID IS NULL ) THEN ( x.DisplayCode + '-' + c13.Description ) WHEN ( Claims.CoverageID = 10 AND C13.CodeID = 10224 AND claims.TypeOfClaimID IS NULL ) THEN ( x.DisplayCode + '-' + c13.Description ) WHEN ( claims.CoverageID = 10 AND claims.CustomCode13ID IS NULL AND t.DisplayCode = 'IO' ) THEN ( x.DisplayCode + '-' + t.Description ) WHEN ( claims.CoverageID = 10 AND c13.CodeID = 10225 AND claims.TypeOfClaimID IS NULL ) THEN ( x.DisplayCode + '-' + c13.Description ) WHEN ( claims.CoverageID = 10 AND c13.CodeID = 10225 AND t.DisplayCode = 'IO' ) THEN ( x.DisplayCode + '-' + c13.Description + '-' + t.DisplayCode ) WHEN ( claims.CoverageID = 10 AND c13.CodeID = 10227 ) THEN ( x.DisplayCode + '-' + c13.Description ) WHEN ( claims.CoverageID = 10 AND c13.CodeID = 10228 ) THEN ( x.DisplayCode + '-' + c13.Description ) WHEN ( x.CoverageID = 10 AND c12.CodeID = 10241 AND c13.CodeID = 10225 AND claims.TypeOfClaimID IS NULL ) THEN ( x.DisplayCode + '-' + c12.Description + '-' + c13.DisplayCode ) WHEN ( x.CoverageID = 10 AND c12.CodeID = 10241 AND c13.CodeID = 10225 AND t.DisplayCode = 'IO' ) THEN ( x.DisplayCode + '-' + c12.Description + '-' + c13.DisplayCode + '-' + t.DisplayCode ) END FROM Claims LEFT JOIN codes c12 ON c12.codeid = claims.customcode12id LEFT JOIN codes c13 ON c13.CodeID = claims.customcode13id LEFT JOIN TypeOfClaims t ON t.TypeOfClaimID = Claims.TypeOfClaimID INNER JOIN Coverages x ON x.CoverageID = Claims.CoverageID Hope this helps. [1]: http://msdn.microsoft.com/en-gb/library/ms181765.aspx
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

clairaustin avatar image clairaustin commented ·
Thank you! That did it.
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.