?
Solved

Cursor definition using a call to Stored Procedure - SQL Server

Posted on 2011-09-16
3
Medium Priority
?
456 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 1000 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 1000 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

Independent Software Vendors: 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

762 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