Created
April 6, 2022 12:38
-
-
Save stevewithington/40161b1b70fef1061bf5f919b055d284 to your computer and use it in GitHub Desktop.
SQL: Execute a list of stored procedures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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