Hi,
How can I turn each row retuned by a SELECT query into a dynamic UPDATE statement and execute one by one?
Option one: use a Cursor
What you describe fits exactly to the use of a Cursor. You can DECLARe a Cursor for the SELECT query which you want. Next you can go row by row using the 'FETCH NEXT FROM' your cursor, and for each row you use UPDATE query
You can also check this post on using cursors
https://www.mssqltips.com/sqlservertip/1599/cursor-in-sql-server/
Note! SQL Server is a tabular server which is bst working on SET of rows and NOT row by row. For moist cases I would HIGHLY recommend NOT to use a cursor or any other type of loop. Instead you can use the next option (which does not cover the requirement for "one by one" but will probably provide better solution
Option two: using UPDATE from SELECT
Use something like the following sam ple:
create table TargetTbl (c1 int, c2 int)
GO
create table SourceTbl (c1 int, c2 int)
GO
INSERT TargetTbl(c1,c2) values (1,1)
INSERT SourceTbl(c1,c2) values (1,2)
GO
SELECT * FROM TargetTbl
SELECT * FROM SourceTbl
GO
update TargetTbl
set c2 = s.c2
from SourceTbl s
GO
SELECT * FROM TargetTbl
SELECT * FROM SourceTbl
GO