maqskywalker
asked on
combining update statements into one statement
I'm using the Northwind database in sql server 2008.
-- Janet Leverling
UPDATE [Northwind].[dbo].[Employe es]
SET [Title] = 'Lawyer'
WHERE [EmployeeID] = 3
-- Robert King
UPDATE [Northwind].[dbo].[Employe es]
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?
-- Janet Leverling
UPDATE [Northwind].[dbo].[Employe
SET [Title] = 'Lawyer'
WHERE [EmployeeID] = 3
-- Robert King
UPDATE [Northwind].[dbo].[Employe
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
possible, yes. Recommended, no.
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:
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
Isn't this much better than complex CASE structure?
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)
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.