Is it possible to iteratively update the same column multiple times - in the same expression?

I have a table that I am getting as a table type parameter in my stored procedure (it comes from excel workbook source, but that's a different story). It has several columns that I need to check the values โ€‹โ€‹against the list of valid values โ€‹โ€‹for each column.

Let's say my table OriginDetails

looks like this (note that this is just a data layout, I have two such tables with every 8 columns that I will be checking) -

Origin   | Status     | Priority | ErrMsg
------------------------------------------
Testing  | In Review  | Low      | 
Design   | Initiated  | Medium   | 
Prod     | Declined   | Critical |           

      

And, I check the values โ€‹โ€‹in the columns Origin

, Status

and Priority

against three different lists (in fact, I check the values โ€‹โ€‹against the data in the tables, but for the sake of simplicity I have hardcoded those values โ€‹โ€‹here) and updating the column ErrMsg

based on my checks -

UPDATE OriginDetails 
SET ErrMsg = ErrMsg + '|Invalid Origin' 
WHERE Origin NOT IN ('Pre-Design','Design','Development') 

UPDATE OriginDetails 
SET ErrMsg = ErrMsg + '|Unrecognized Status' 
WHERE Status NOT IN ('In Review','Approved') 

UPDATE OriginDetails 
SET ErrMsg = ErrMsg + '|Priority check failed' 
WHERE Priority NOT IN ('Critical','Medium','High')

      

Everything is great and great, works great, but I get 16 of these update statements for 2 tables together, so I have a really big and ugly block of code (and a lot of duplication too, since I have almost the same code for 2 tables).

Is there a way to do all the updates in one document for each table?

Something like below, except that it must fulfill each of the conditions instead of just one -

UPDATE OriginDetails
SET ErrMsg = ErrMsg + 
(CASE WHEN Origin NOT IN ('Pre-Design','Design','Development') 
           THEN '|Invalid Origin'
      WHEN Status NOT IN ('In Review','Approved') 
           THEN '|Unrecognized Status'
      WHEN Priority NOT IN ('Critical','Medium','High') 
           THEN '|Priority check failed'
END)

      

Any ideas / direction are appreciated. Thank.

+3


source to share


2 answers


Something like this should work well (and only need to enter values โ€‹โ€‹at a time):

UPDATE OriginDetails
SET ErrMsg = ErrMsg + 
    CASE WHEN Origin NOT IN ('Pre-Design','Design','Development') THEN '|Invalid Origin' ELSE '' END
    + CASE WHEN Status NOT IN ('In Review','Approved') THEN '|Unrecognized Status'  ELSE '' END
    + CASE WHEN Priority NOT IN ('Critical','Medium','High')  THEN '|Priority check'  ELSE '' END

      



And here is the SQL Fiddle .

Good luck.

+6


source


How about this:

UPDATE OriginDetails
SET ErrMsg = ErrMsg + 
(CASE 
WHEN Origin NOT IN ('Pre-Design','Design','Development') and Status NOT IN ('In Review','Approved') and Priority NOT IN ('Critical','Medium','High') 
           THEN '|Invalid Origin|Unrecognized Status|Priority check failed'
WHEN 
Origin NOT IN ('Pre-Design','Design','Development') and Status NOT IN ('In Review','Approved') and Priority IN ('Critical','Medium','High') 
           THEN '|Invalid Origin|Unrecognized Status'


END)

      



you can add all cases like me.

+1


source







All Articles