Link to home
Start Free TrialLog in
Avatar of Stephen Forero
Stephen ForeroFlag for United States of America

asked on

foreach in SQL

Hi all,

I want to run a SP (shown below)...

for every row in

SELECT * FROM mainDB WHERE X = 'hi'
(say for example this brings in 2000 rows)

feeding in 2 columns of variables...

STORED PROCEDURE......
EXEC dbo.DailyPositions_ProductContractTimePeriod 
@Product = (COLUMN1 of main select statement), 
@AccountID = 'D6323759-1E95-4354-85A1-7040EA6F5FD8', 
@ContractDate = (COLUMN2 of main select statement), 
@StartDate = '20130510', 
@EndDate = '20130510'

Open in new window



can someone point me in the right direction?

I see things online like PIVOT/CURSOR/ROWNEXT..

thanks
SOLUTION
Avatar of dsacker
dsacker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I assume, the objective is:

1 get 1st record in a table.
2 assign:  @c1 to col1 and @c2 to col2 of this record.
3 Use @c1, @c2 in some operation.
4 move to next record
5 repeat from step 2 until end of records.

Is this correct?
Avatar of Stephen Forero

ASKER

thanks guys... appreciate the help