Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure Question?? Please HELP!!

Author  Topic 

jblanco2281
Starting Member

4 Posts

Posted - 2010-09-10 : 15:18:34
I am writing a stored procedure in which I call another one that returns me ONE row of a table... How can I use the field values of that returned row in my Stored Procedure?? I have to use a cursor or can I avoid the cursor because I have to run this SP about 50 times per sec so I have read that cursors can be very resource consuming...


Thanks for your help...

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-10 : 15:23:48
You can put the row of data into a temp table.

INSERT INTO #Temp
EXEC YourStoredProc

Or you can modify the stored proc so that it uses output parameters for each column returned.

The only way to avoid the cursor if you need to call it 50 times is to rewrite the stored proc so it returns you all of the data in one short, and not one row at a time.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jblanco2281
Starting Member

4 Posts

Posted - 2010-09-10 : 15:31:06
Thanks man, how do I modify the stored procedure to return Output parameters??? I think that would be the best solution and how do I assign those returned output parameters into variables???
Go to Top of Page

jblanco2281
Starting Member

4 Posts

Posted - 2010-09-10 : 15:41:35
Sorry about the man... I just noticed you are a woman...
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-10 : 15:47:56
Here's an example:

CREATE PROC sp1 (@var1 int, @var2 int OUTPUT, @var3 varchar(50) OUTPUT)
AS

SELECT @var2 = ColumnA, @var3 = ColumnB
FROM tbl1
WHERE Column1 = @var1
GO

DECLARE @newVar1 int, @newVar2 int, @newVar3 varchar(50) --the variables can be the same name, I just used new ones to make it clear.

EXEC sp1 @var1 = @newVar1, @newVar2 OUTPUT, @newVar3 OUTPUT

You've now got the values loaded into variables.

Hope this was clear!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jblanco2281
Starting Member

4 Posts

Posted - 2010-09-10 : 15:53:19
Thank you very much I think this really gave me a clear idea of what to do...
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-10 : 15:55:44
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -