Solved

SQL Server 2008 Stored Procedure - updating multiple tables

Posted on 2010-09-19
4
779 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:dodgerfan
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33713419
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

0
 

Author Comment

by:dodgerfan
ID: 33713460
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.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33713599
I'm thinking something like this

create procedure [dbo].[returnBook]
@rentid int,
@memberId int,
@returneddate datetime,
@bookid Int
AS
UPDATE bookrental
SET
returneddate=@returneddate
WHERE rentId=@rentid And memberId = @memberId

insert bookrental (bookID, memberId, rentId, rentdate)
select top(1) bookID, @memberId, @rentId, getdate() -- mix of values from vwNextBook and variables
from vwNextBook where memberId = @memberId
0
 

Author Closing Comment

by:dodgerfan
ID: 33713717
Thanks for the help. I got it working right with your help.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help how to find where my error is in UFD 6 47
Query group by data in SQL Server - cursor? 3 65
How to keep a record with the highest value 3 42
sql query 5 44
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question