Sql-server – How to run update queries saved in a permanent table

sql serverssms

Permanent Table Name: PermaTable1
Values:
enter image description here

Question is how to execute all update commands inside the table PermaTable1?

This is my query but it does not execute the update command.

DECLARE @Query1 Nvarchar(MAX)

SET @Query1= 'SELECT Query FROM dbo.PermaTable1'

EXEC(@Query1)

Hope you can help me. Thank you.

Best Answer

DECLARE @Query1 nvarchar(max) = N'';

SELECT @Query1 += Query + N';' FROM dbo.PermaTable1;

PRINT @Query1; -- to see the first 8,192 characters

-- EXEC sys.sp_executesql @Query1; -- uncomment this when you trust it

But I have questions. Shouldn't those update queries have where clauses? Otherwise each one is going to overwrite all rows and basically undo all the updates that came before it. Also are these update commands really stored in the same table they affect?