Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Created April 6, 2022 12:38
Show Gist options
  • Save stevewithington/40161b1b70fef1061bf5f919b055d284 to your computer and use it in GitHub Desktop.
Save stevewithington/40161b1b70fef1061bf5f919b055d284 to your computer and use it in GitHub Desktop.
SQL: Execute a list of stored procedures
USE [someDb];
GO;
DECLARE @table Table (id INT IDENTITY(1,1), item NVARCHAR(255) NOT NULL)
;
INSERT INTO @table (item)
SELECT someColumn
FROM someConfigurationTable
WHERE anotherColumn = 'stored_proc'
ORDER BY someColumn
;
DECLARE
@x NVARCHAR(255)
, @idx INT = 0
, @totalRecords INT = (SELECT COUNT(*) FROM @table)
;
WHILE @idx < @totalRecords
BEGIN
SET @idx = @idx + 1;
SET @x = (SELECT item FROM @table WHERE id = @idx);
PRINT 'Executing: ' + @x -- output the stored proc that will be executed
EXEC (@x); -- the parenthesis are necessary
END
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment