Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

how to browse through recordset in stored procedure

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
ronipats
Asked:
ronipats
1 Solution
 
Surendra NathTechnology LeadCommented:
you have to use CURSORs in the stored procedure

the below link might help
http://msdn.microsoft.com/en-us/library/ms180169.aspx
0
 
Ephraim WangoyaCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now