Can a FOR/NEXT type loop be done in a SQL Stored Procedure

I need to code a for next loop in a SQL stored procedure.  I tried joining multiple SELECT statements together but the length of the string is greater then 8000.   Basically the SELECT statement returns the same 3 values, only the where clause changes.  Looking for a value equal to an specific number that is then incremented 10 times, ie WHERE ItemCount = 10,  WHERE ItemCount = 11 etc.  Looking to loop and increcment the value.  I am assignng the results to a temp table and then retreiveing them at the end of the process.

If it is possible what is the syntax?

Thank you
skinsfan99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

cmgarnettCommented:
DECLARE @counter INT

SET @counter = 1

WHILE @counter <= 10
BEGIN

SELECT A, B, C FROM MyTable WHERE ItemCount= @counter

SET @counter = (@counter + 1)

END
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
wls3Commented:
Here is an approach I use all the time now:

http://learningpcs.blogspot.com/2010/02/tsql-rowcount.html

As outlined in the link, you use @@ROWCOUNT immediately after your select statement to gather the data.  Also, in 2008, you can join multiple lines with EXEC(@Data1 + @Data2 + @Data3 + ... + @Data10).  But, even easier is VARCHAR(MAX):

http://msdn.microsoft.com/en-us/library/ms178158.aspx
0
skinsfan99Author Commented:
Thank you!!
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

From novice to tech pro — start learning today.