View Single Post
  #2 (permalink)  
Old Jun 29th, 2007, 18:57
spinal007's Avatar
spinal007 spinal007 is offline
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,612
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Update two tables using Stored Procedure in SQL

What's the exact situation? Is this a one off or something that will happen on a regular basis?

FACT: You can only update one table at a time (you can use views to give the impression that multiple tables are updated at once but we don't need to go there).

Trigger or SP?
A trigger is essentially a stored procedure that gets fired in response to an event (insert/update/detele).

But if you use a stored procedure you will need to 'manually' call it after updating one of the tables.

Just use whatever method you're more comfortable with.
How do you do it?
PROCEDURE:
You'll need something like this...
Code: Select all
CREATE PROCEDURE dbo.UpdateTables(@value nvarchar(100)) AS
BEGIN
 UPDATE TABLE1 SET field1 = @value
 UPDATE TABLE2 SET field2 = @value
END
Add parameters and use the WHERE clause to update only certain records...

TRIGGER:
You will need something like...
Code: Select all
CREATE TRIGGER [TABLE1Trigger] ON [dbo].[TABLE1]
FOR INSERT, UPDATE, DELETE -- if applicable
AS
 UPDATE TABLE2 SET field2 = (SELECT field1 FROM TABLE1)
END
You actually have to use WHERE the clause in this case but you get the idea...

Hope this helps
Reply With Quote