Solved

Updating multiple columns in a single update transaction

Posted on 2013-12-10
2
262 Views
Last Modified: 2013-12-10
I have a table with many columns.  Some have a default value of 0 and need to set it to null (after removing the constraint of course).

Rather than have multiple update statements that look like this:

update table1 set column1 = null where column1 = 0
update table1 set column2 = null where column2 = 0

Is there a way to wrap these up into a single update statement or transaction with multiple where clauses?
0
Comment
Question by:ccleebelt
[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
2 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39710310
update table1 set
  column1 = case when column1 = 0 then null else column1 end
  , column2 = case when column2 = 0 then null else column2 end
0
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 39710548
I would perhaps add a WHERE clause to restrict the number of scanned rows, like this:
update table1 set
  column1 = case when column1 = 0 then null else column1 end
  , column2 = case when column2 = 0 then null else column2 end 
WHERE column1 = 0 OR column2 = 0

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

734 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