combining update statements into one statement

maqskywalker
maqskywalker used Ask the Experts™
on
I'm using the Northwind database  in sql server 2008.

-- Janet Leverling
UPDATE [Northwind].[dbo].[Employees]
SET [Title] = 'Lawyer'
WHERE [EmployeeID] = 3

-- Robert King
UPDATE [Northwind].[dbo].[Employees]
SET [Title] = 'Doctor'
WHERE [EmployeeID] = 7

I have two update statements like this.
When I run them individually they work fine.

I want to put these in a stored procedure, so when I run the stored procedure both records get updated.

Is there a syntax to combine both these statements into one statement?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
There is:

UPDATE [Northwind].[dbo].[Employees]
SET [Title] = CASE EmployeeID WHEN 3 THEN 'Lawyer' WHEN 7 THEN 'Doctor' ELSE Title END
WHERE [EmployeeID] IN (3, 7)
This is very bad approach.  Just imagine the requirement to update 10 or more rows...

Why it should be necessary to create "complex" expression in the UPDATE command when you can simply issue several UPDATE commands in one SP? And the above CASE structure also contains unnecessary ELSE part.

And if you need to update either both rows or none then enclose the two or more UPDATE statements into a TRANSACTION and handle possible errors.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
possible, yes.  Recommended, no.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
This is very bad approach.

Not necessarily.  Every separate UPDATE is additional overhead to write and to have SQL parse, prepare to exec, etc..

It depends on your specific situation.

And if you need to update either both rows or none then enclose the two or more UPDATE statements into a TRANSACTION and handle possible errors.

What if you're already in a transaction?  Then it gets more complex.
Simple UPDATE of one row based on the PK means almost nothing from the performance point of the view.

OK, the right solution for more updates at once is to prepare a (temp) table containing all updates and then you may do it in one UPDATE command:
DECLARE @NewTitles TABLE (Id int, title varchar(20))
INSERT INTO @NewTitles VALUES
(3, 'Lawyer'), 
(7, 'Doctor')

UPDATE Northwind.dbo.Employees SET Title = t.Title
  FROM Northwind.dbo.Employees e
  JOIN @NewTitles t ON t.ID = e.EmployeeID

Open in new window

Isn't this much better than complex CASE structure?
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
Isn't this much better than complex CASE structure?

Using a table variable?  Usually not.  Table variables often generate horrible plans.

Using a temp table?  It's ok, but again, look at all the additional overhead:
create a separate table structure;
load rows to that table structure;
join that table (you could at least key the temp table on the ID; may help, may not, but it definitely won't hurt)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial