Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SQL Temp Table Loop
-- This can be performed numerous ways, and can also be performed more efficiently, but I broke out the
-- sections so that they are easy to identify and understand
-- create temp table
declare @tbl table (id int identity, valueA int)
-- used for the loop
declare @ptr int, @max int, @valueA int
-- insert list of ID's into temp table
insert into @tbl (valueA) select valueA from someTable
-- set the loop values to start
set @ptr = 1
set @max = (select max(id) from @tbl)
-- start the loop
while @ptr <= @max
select @valueA = valueA from @tbl where id = @ptr
exec prc_DoSomething @valueA -- execute fix procedure or do something
set @ptr = @ptr + 1 -- next record
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.