SQL - execute query from table cells
This should help you go through all scripts in that table. With what you are doing, only one script will be run so if the table has 100 Scripts, only one will be executed. Hope this helps
DECLARE @Queries TABLE (ID INT IDENTITY(1,1),SQLScript VARCHAR(MAX))
DECLARE @STR_QUERY VARCHAR(MAX);
DECLARE @StartLoop INT
DECLARE @EndLoop INT
INSERT INTO @Queries
SELECT Query
FROM [AccHelper].[dbo].[Queries]
SELECT @EndLoop = MAX(ID), @StartLoop = MIN(ID)
FROM @Queries
WHILE @StartLoop < = @EndLoop
BEGIN
SELECT @STR_QUERY = SQLScript
FROM @Queries
WHERE ID = @StartLoop
EXEC (@STR_QUERY)
SET @StartLoop = @StartLoop + 1
END
Try below:
DECLARE @STR_QUERY NVARCHAR(max);
SET @STR_QUERY = (SELECT Query FROM [AccHelper].[dbo].[Queries])
EXECUTE SP_EXECUTESQL @STR_QUERY