• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

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
0
solarissf
Asked:
solarissf
2 Solutions
 
dsackerContract ERP Admin/ConsultantCommented:
You'll have to run that via a CURSOR:
DECLARE @COLUMN1    varchar(80),    -- or whatever is your true datatype
        @COLUMN2    varchar(80)     -- or whatever is your true datatype

DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT COLUMN1, COLUMN2 FROM mainDB WHERE X = 'hi' 

OPEN myCursor

WHILE 1 = 1
BEGIN
    FETCH FROM myCursor INTO @COLUMN1, @COLUMN2
    IF @@FETCH_STATUS <> 0 BREAK

    EXEC dbo.DailyPositions_ProductContractTimePeriod 
        @Product = @COLUMN1, 
        @AccountID = 'D6323759-1E95-4354-85A1-7040EA6F5FD8', 
        @ContractDate = @COLUMN2, 
        @StartDate = '20130510', 
        @EndDate = '20130510'
END

CLOSE myCursor
DEALLOCATE myCursor

Open in new window

0
 
Ross TurnerCommented:
Hi solarissf

i create the below example script... which is basically a loop with dynamic sql

1) select your criteria into a temp table giving it an identity field e.g id
2) uses count on the temptable to find the amount of loops.
3) assigns the column1 / column2 on each pass based of the incrementing id

if you use this in your test enviroment it will just print the dynamic sql.

but if you want it to execute just uncomment the -- exec (@sql)


Set nocount on 

IF OBJECT_ID('tempdb..#mainDB ') IS NOT NULL
DROP TABLE #mainDB 
Create Table #mainDB 
( Column1 varchar(30),
  Column2 varchar(30)
)

INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test1','Column2-Test1');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test2','Column2-Test2');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test3','Column2-Test3');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test4','Column2-Test4');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test5','Column2-Test5');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test6','Column2-Test6');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test7','Column2-Test7');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test8','Column2-Test8');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test9','Column2-Test9');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test10','Column2-Test10');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test11','Column2-Test11');
INSERT INTO #mainDB(Column1,Column2) VALUES('Column1-Test12','Column2-Test12');



IF OBJECT_ID('tempdb..#TempTableLoop') IS NOT NULL
DROP TABLE #TempTableLoop
Create Table #TempTableLoop(
ID int identity, 
Column1 varchar(30),
Column2 varchar(30)
)

INSERT INTO #TempTableLoop (Column1,Column2)
SELECT  *
FROM    #mainDB
-- where what ever your criteria

DECLARE @Count	int
DECLARE @SQL	Varchar(8000)
DECLARE @COLUMN1	Varchar(8000)
DECLARE @COLUMN2	Varchar(8000)
SET		@Count = 1

While @count < (Select count(*) from #TempTableLoop)
Begin 
Set @COLUMN1 = (select column1 from #TempTableLoop where id = @Count)
Set @COLUMN2 = (select column2 from #TempTableLoop where id = @Count)

Set @Sql =  '
		EXEC dbo.DailyPositions_ProductContractTimePeriod 
        @Product =''' + @COLUMN1 + '''
		@AccountID = ''D6323759-1E95-4354-85A1-7040EA6F5FD8''' +
        ',@ContractDate =''' + @COLUMN2 + '''' +
		',@StartDate = ''20130510''' +
		',@EndDate = ''20130510'''
        
print @sql
-- exec (@sql)

Set @Count = @Count + 1
End

Open in new window

0
 
hnasrCommented:
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?
0
 
solarissfAuthor Commented:
thanks guys... appreciate the help
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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