Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through variables for multiple queries 1

Status
Not open for further replies.
Sep 14, 2010
5
US
I have a fairly complex query I run 7 times every morning. I have declared variable that I have to change 7 times, one for each query. I'm trying to figure out how to loop through the query 7 times, changing the variable in question each time, and output the results to a single table. A while loop seems to increment values, but my variable I need to change is a 3 digit character code.

Any help would be appreciated.

Thanks,
Don
 
Something like...

Code:
DECLARE @Iteration INT = 0
DECLARE @MyVariable CHAR(3)

WHILE (@Iteration < 7) BEGIN

	SET @Iteration = @Iteration + 1
	
	SET @MyVariable =
	CASE @Iteration
	   WHEN 1 THEN 'ABC'
	   WHEN 2 THEN 'DO2'
	   WHEN 3 THEN 'DO3'
	   WHEN 4 THEN 'DO4'
	   WHEN 5 THEN 'DO5'
	   WHEN 6 THEN 'DO6'
	   WHEN 7 THEN 'XYZ'
	END
	
	-- Use @MyVariable in your query here
	
END
 
Thanks Dave,
So simple, yet I couldn't see it through my pre-conceived notion of what it should be.

Don
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top