Solved

foreach in SQL

Posted on 2013-05-13
4
189 Views
Last Modified: 2013-05-13
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
Comment
Question by:solarissf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 20

Assisted Solution

by:dsacker
dsacker earned 250 total points
ID: 39162059
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
 
LVL 7

Accepted Solution

by:
Ross Turner earned 250 total points
ID: 39162180
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
 
LVL 30

Expert Comment

by:hnasr
ID: 39162714
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
 

Author Comment

by:solarissf
ID: 39162873
thanks guys... appreciate the help
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question