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?
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
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)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcelbaCommented:
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 LLCCommented:
possible, yes.  Recommended, no.
Maximize Customer Retention with Superior Service

The IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more to help build customer satisfaction and retention.

Scott PletcherSenior DBACommented:
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.
pcelbaCommented:
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 DBACommented:
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)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.