Solved

how to browse through recordset in stored procedure

Posted on 2013-06-22
2
325 Views
Last Modified: 2013-07-17
Hi,

I want to write one stored procedure with select statement and i want to traverse through each record as we do in programming using while look. How can i do that.

i.e. say i want to execute sql statement SELECT * FROM <TABLE NAME> and then i want to browse through resultant recordset, one by one record in stored procedure. how can i do that.
0
Comment
Question by:ronipats
[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
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39268878
you have to use CURSORs in the stored procedure

the below link might help
http://msdn.microsoft.com/en-us/library/ms180169.aspx
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 39269000
There is no need for a cursor
Suppose your table has primary key ID and you want to go through the field FirstName

declare @ID int
declare @FirstName varchar(15)

set @ID = 0
while EXISTS(select top 1 1 from Table where ID >= @ID)
begin
  select top 1 @ID = ID, @FirstName = FirstName
  from Table
  where ID >= @ID

  set @ID = @ID + 1

  --do stuff with your variable @FirstName
end

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

631 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