Solved

Sql Server - Update Multiple Column based on a condition

Posted on 2014-01-28
6
1,945 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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:
ScottPletcher 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

757 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

22 Experts available now in Live!

Get 1:1 Help Now