Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sql Server - Update Multiple Column based on a condition

Posted on 2014-01-28
6
Medium Priority
?
2,645 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

722 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