Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

asked on

SQL Server 2008 Stored Procedure - updating multiple tables

I have a stored procedure that updates the data in a table based on input values. Once that part is done, I want the procedure to take one of the input parameters and return a record from a view, a different recordset. Using the data returned from the view, I want to update another table. Is there a way to do this?
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

The information is so vague, hope this helps.
Steps 2 and 3 can be done in one query, instead of retrieving and storing it temporarily.
create proc myprocedure
@input1 int,
@input2 varchar(20),
@input3 int,
@key uniqueidentifier,
@key2 int
AS
set nocount on;

-- update a table
update tbl1 set col1 = @input1 where primary_key = @key;

-- update another table using values from "a different recordset"
update tbl3 set
	colA = v.sourcecol1,
	colB = v.sourcecol2
from view_record v
where v.col2 = @input2
  and tbl3.primary_key = @key2;
GO

Open in new window

Avatar of dodgerfan

ASKER

Yes, sorry about the vagueness. I should have been more detailed. My current sp looks lke this:
create procedure [dbo].[returnBook]
@rentid int,
@memberId int,
@returneddate datetime,
@bookid Int
AS
UPDATE bookrental
SET
returneddate=@returneddate
WHERE rentId=@rentalId And memberId = @memberId

This works. Now I need to open another query to find the next book for that member, call vwNextBook with the memberid being the value. Then I need to take that data and insert into the bookrental table, with some of the data already captured in the original variables, too. So take the bookId returned in this query and insert it with memberid and a new date into the oriiginal bookrental table. I think this makes sense. I will try what you have already posted, too.
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help. I got it working right with your help.