For loops in SQL
For loops in SQL
(OP)
I want to iterate through a list of key values and run SQL in each iteration based on that value but im not sure how to use the for loop in SQL. Something like this:
declare @test numeric(9)
for @test in (134424,134423,134425)
select count(*) from test_table where test_id=@test
next
declare @test numeric(9)
for @test in (134424,134423,134425)
select count(*) from test_table where test_id=@test
next
RE: For loops in SQL
RE: For loops in SQL
RE: For loops in SQL
CODE
DECLARE reader CURSOR FOR SELECT fname FROM #dir
OPEN reader
FETCH NEXT FROM reader INTO @fetcher
WHILE @@FETCH_STATUS=0
BEGIN
...Do whatever you want in the loop!...
FETCH NEXT FROM reader INTO @fetcher
END