Solved

Cursor definition using a call to Stored Procedure - SQL Server

Posted on 2011-09-16
3
449 Views
Last Modified: 2012-05-12
Hi,

Can i write the Cursor definition using a call to Stored Procedure instead of SELECT statement ? BTW: Stored procedure returns records (Same as SELECT statement)

Here is what i need:

declare C1 cursor for EXEC spSelPersonsDetails @StartPersonId, @EndPersonId, @Active

-- Continue with cursor

open C1

Thanks
0
Comment
Question by:milani_lucie
3 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 250 total points
ID: 36550546
no
 unless the stored procedure itself returns a cursor (cursor variable)

however you can call the stored procedure and have its result set inserted into a temp table and then process that.

insert into #temp (list of columns)
 exec yourprocedure @parem1,@paarm2,....


are you sure you need to process the results using a cursor ?
there are usually better(faster,less resource intensive) ways of processing a set of rows...

what do you need to do with the returned rows?
0
 

Author Comment

by:milani_lucie
ID: 36550639
I know that we can use WHILE loop instead of cursor. Are there any other ways to do ?

Thanks
0
 
LVL 21

Assisted Solution

by:JestersGrind
JestersGrind earned 250 total points
ID: 36551205
A cursor or while loop are both loops.  Loops should generally be avoided in SQL because SQL is designed to handle sets of data.  It's always best to employ set-based solutions instead of loops also known as RBAR(Row By Agonizing Row).  If you're inserting/updating/deleting data, you can probably handle all of the records at the same time  instead of one at a time.  If you're doing something like passing the values of each row to a stored procedure, you would have to loop, unless you can rewrite the stored procedure to handle a table of data via a user defined table type or XML.   Depends on what you are doing with the data.

Greg

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

770 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