Last active
December 29, 2015 09:29
-
-
Save alexdwhite/7650282 to your computer and use it in GitHub Desktop.
SQL Temp Table Loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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