Solved

Sql Server - Update Multiple Column based on a condition

Posted on 2014-01-28
6
2,334 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 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CASE Statement using different fields 8 33
Sorting a SQL script 5 41
Begin Transaction 12 26
Enable TLS 1.2 for SQL 2012 Web Edition 1 24
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 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