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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 can i declare a cursor on a stored prcoedure???

Author  Topic 

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2010-03-16 : 14:49:33
Hello,

Very quick: Can I declare a cursor on a stored procedure?

I am using someone else's stored procedure which, when executed, displays a table of results. The only way I think I can manipulate the table, without modifying the stored procedure, is to use cursors. I tried declaring a cursor for the stored procedure, where one would normally put a select statement, but it's not working. Again, the procedure I wish to obtain results from cannot be modified. I have to code around it. Any suggestions would be most welcome.

Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-16 : 15:25:10
I'm not sure if you can do it directly, but a simple way is to put the result of the stored procedure into a temp table and then cursor through that.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 01:43:58
why do you need a cursor? Is it not possible to achieve your result using set based solution after you get procedure result onto a temp table as Tara suggested

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-17 : 16:22:15
You can only use a set-based approach if the stored procedure can be changed or if the user can get to the data in another way. SQLIsTheDevil, must you use that stored procedure and without any changes?

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

venkat09
Starting Member

17 Posts

Posted - 2010-03-31 : 07:35:47
The answer is NO.

You can do this.

create table #TempTbl (col1 datatype1, col2 datatype2, ....)
insert into #TempTbl (col1 datatype1, col2 datatype2, ....) exec someOneElse'storedProcedure

/*Of course the temp table should have the same columns in the same order as that of the result-set of "someOneElse'sStoredProcedure"*/

declare cur Cursor on select col1, col2, ... from #TempTbl
....
Your code
....
....

Ofcourse using a cursor is the least efficient way. You could manipulate the results in, an update statement with using what ever condition you want to use on any number of subsets of the result-set/tablular results.

Venkat R. Prasad
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-03-31 : 07:55:35
Don't use a cursor unless you absolutely have to. They're as slow as buggery.

There are 10 types of people in the world, those that understand binary, and those that don't.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2010-03-31 : 07:57:00
Wish we had method overriding and interfaces in SQL Server But then again, beauty of SQL will be lost!

Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page
   

- Advertisement -