Wednesday, March 21, 2012

How to loop through a table?

Hi,
I have a stored procedure, SPUpdateStatus, with a parameter,
InstanceID.
InstanceID is a column in a table, "BatchInstance". I'll need to loop
through table, "BatchInstance", and execute SPUpdateStatus on each
InstanceID. What's the syntax for the loop?
Thanks!On 21 Mar, 15:35, "Curious" <fir5tsi...@.yahoo.com> wrote:
> Hi,
> I have a stored procedure, SPUpdateStatus, with a parameter,
> InstanceID.
> InstanceID is a column in a table, "BatchInstance". I'll need to loop
> through table, "BatchInstance", and execute SPUpdateStatus on each
> InstanceID. What's the syntax for the loop?
> Thanks!
Why not just amend SPUpdateStatus or create a new proc so that it can
perform the same operation on all rows at once? That way you won't
need a slow, complex and cumbersome loop to do the job.
If you aren't willing or able to do that then lookup the DECLARE
CURSOR syntax in Books Online. There are lots of reasons why cursors
are almost always a bad idea for data manipulation operations. You
should carefully consider the alternatives first.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

No comments:

Post a Comment