Solved

Sql Server - Update Multiple Column based on a condition

Posted on 2014-01-28
6
2,098 Views
Last Modified: 2014-02-21
Experts, in SQL Server 2012, based on a conditional statement, if the condition is true, then I need to update more than one column.

Is there a way to do it without repeating the conditional statement for every column?

What would a basic template for the query look like?


Thanks in advance.
0
Comment
Question by:bobinorlando
  • 3
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39815816
UPDATE YourTable
SET ColumOne = 'banana', ColumnTwo = 'red'
WHERE pigs_fly = True

Can you expand on your question, preferably with some sample data?
0
 
LVL 1

Author Comment

by:bobinorlando
ID: 39815855
Ok I have multiple conditions I need to test (several dozen) and then perform the appropriate updates. I'm looking for the most efficient way to do this to save time.

I'm looking for some outer logic that can enclose the update statements.

e.g if SQL  were procedural I would test first and then set the column values.

If <condtion1> = true
Then
set col1 = 'a', col2 = 'b'
elseif <condition2> = true
Then
set col1 ='j', col2= 'z'

I'd love to use Set statements inside of a Case statement but that is not allowed.
e.g.

CASE
WHEN <condition1> = true
THEN set col1 = 'a', col2 ='b'
WHEN <condition2> = true
THEN set col1 = 'j', col2 ='z'
Else
END
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39815996
Not abundantly following you, but yes you can use CASE with an update to change one column based on the condition or value in the other.

UPDATE YourTable
SET Column1 = CASE Column2 WHEN 'goo' then 1 WHEN 'foo' THEN 2 WHEN 'boo' THEN 3 END
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 1

Author Comment

by:bobinorlando
ID: 39816021
Yes a Set col = CASE WHEN THEN ELSE END works fine for one column.

Any way to avoid putting the conditional logic in a where clause when updating multiple columns?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39816037
>Any way to avoid putting the conditional logic in a where clause when updating multiple columns?

Still not sure what you're getting at.  Show us a 'before and after' dataset.

Having a WHERE clause would apply that condition to all columns being updated.
Having CASE blocks in the SET would make them specific to each column.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39816574
UPDATE tn
SET
    col1 = CASE
        WHEN condition# = 1 THEN 'a'
        WHEN condition# = 2 THEN 'j'
        ...
        END,
    col2 = CASE
        WHEN condition# = 1 THEN 'b'
        WHEN condition# = 2 THEN 'z'
        ...
        END
    --,...
FROM dbo.tablename tn
CROSS APPLY (
    SELECT CASE
        WHEN <condition1> = true THEN 1
        WHEN <conidtion2> = true THEN 2
        WHEN <condition3> = true THEN 3
        ELSE 0 END AS condition#
) AS ca1
WHERE
    ca1.condition# > 0
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now