We help IT Professionals succeed at work.

combining update statements into one statement

124 Views
Last Modified: 2019-01-15
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

Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:
possible, yes.  Recommended, no.
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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.
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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)