Solved

Cursor definition using a call to Stored Procedure - SQL Server

Posted on 2011-09-16
3
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 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