Solved

Sql Server - Update Multiple Column based on a condition

Posted on 2014-01-28
6
2,514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 66

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 66

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 66

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

630 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