Solved

Cursor definition using a call to Stored Procedure - SQL Server

Posted on 2011-09-16
3
447 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now