SQL Loop with a Variable

Hi

I have a 7 queries that run based on a variable.... the variable can change several times...I want to add a loop to the query so it skips through each of the codes ...
ie:
Declare @Code as VARCHAR(3)
Declare @Month as VARCHAR(10)

Set @Month =  'Month 1' ............this stays in stone till the following month
Set @Code = 'AAA'

Now there is a table called tbl_test
that has a list of 7 codes, AAA, BBB, CCC, etc...

There are several Insert queries that are reliant on the @Code

So I want the SQL to loop through the tbl_test .....
flickimp1717Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

indianguru2Commented:
See the script below.
-- Dump the data from the Table into a Temp Table

SELECT  IDENTITY( NUMERIC(18), 1, 1 ) AS Seq ,
        Code
INTO    #tmpT
FROM    tbl_test 

DECLARE @RowNum INT ,
    @RowCount INT
SET @RowNum = 1
SET @RowCount = ( SELECT COUNT (1) FROM #tmpT
                )
                       
IF @RowCount > 0 
    BEGIN
        WHILE ( @RowNum <= ( SELECT MAX (Seq) FROM #tmpT
                           ) ) 
            BEGIN
                DECLARE @Code AS VARCHAR(3)
                DECLARE @Month AS VARCHAR(10)

                SET @Month = 'Month 1'
                --- Get the Code from the table here.
                SELECT  @Code = Code
                FROM    #tmpT
                WHERE   Seq = @RowNum
                
                
                --- Do Your thing here.
                
                
                SET @RowNum = @RowNum + 1
            END
    END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
flickimp1717Author Commented:
Thanks for that
Wil have a go and let you know how it goes
0
flickimp1717Author Commented:
I'm a little confused over how the loop bit works and if I really need to sue a temp table?

What I require is....

@Code = AAA
then run all queries

then @Code = BBB
then run all queries

etc....
0
flickimp1717Author Commented:
Sorry, the code here is excellent and does the job

Many thanks
0
indianguru2Commented:
The temp table is used for the Unique Identity Column which it auto generates and impersonates as a row number which is the easiest solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.