Skip to content

Instantly share code, notes, and snippets.

@alexdwhite
Last active December 29, 2015 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdwhite/7650282 to your computer and use it in GitHub Desktop.
Save alexdwhite/7650282 to your computer and use it in GitHub Desktop.
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
begin
select @valueA = valueA from @tbl where id = @ptr
exec prc_DoSomething @valueA -- execute fix procedure or do something
set @ptr = @ptr + 1 -- next record
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment