Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 loop through items in sp

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2009-01-18 : 04:18:32
How can I within a stored procedure loop though items and then execute another stored procedure

for example
I want to

select amount from useramounts where amount>@amount (variable in sp)
then loop through this select
@ do exec calculategain @id=useramountid

please advise

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-18 : 04:33:25
1) Use a cursor
2) Use a WHILE loop



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-18 : 08:56:25
quote:
Originally posted by esthera

How can I within a stored procedure loop though items and then execute another stored procedure

for example
I want to

select amount from useramounts where amount>@amount (variable in sp)
then loop through this select
@ do exec calculategain @id=useramountid

please advise


isnt it better to make this a udf so that you can avoid loop and simply call it from select. something like

select amount,dbo.calculategain(useramountid) from useramounts where amount>@amount
Go to Top of Page
   

- Advertisement -