Avatar of maqskywalker
maqskywalker
 asked on

combining update statements into one statement

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?
Microsoft SQL Server 2008Microsoft SQL Server

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Pavel Celba

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 Fye

possible, yes.  Recommended, no.
Scott Pletcher

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Pavel Celba

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 Pletcher

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)