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.
source to share
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.
source to share
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.