I have a table that I need to write a migration script for to move some data from one table to another. To do this I want to write a script that allows me to iterate over each row in a particular table and then store the values in variables before inserting them into another table.
I have a script so far but at the moment it isn't iterating over each row, rather its checking the same row each time. The rows in the table have a GUID as their Id, so it's not just a case of checking the id of the row against the index in the while loop.
At the moment I'm printing out X rows but each print is identical because the script isn't iterating.
What do I need to do to allow my script to iterate over each row instead?
declare @i int = 1
declare @numRows int
declare @settingId nvarchar(255) = ''
declare @dynamicValue nvarchar(255) = ''
declare @ref nvarchar(255) = ''
select @numRows = COUNT(*) from [set].Settings
while(@i <= @numRows)
begin
select @settingId = Id, @dynamicValue = DynamicValue, @ref = Ref
from [set].Settings
print @settingId + ' : ' + @dynamicValue + ' : ' + @ref
set @i = @i + 1
end
Edit: The purpose of the script is to lift some data from the table it is currently in, manipulate the ref slightly and then insert the data back into another table with the same (or modified) values.