Untitled
-- Global temporary table to store results (accessible in dynamic SQL) IF OBJECT_ID('tempdb..##Results') IS NOT NULL DROP TABLE ##Results; CREATE TABLE ##Results ( ProcedureID NVARCHAR(100), -- Procedure name Total INT -- The "total" value (0 or 1) ); -- Procedure names DECLARE @ProcedureList TABLE (ProcedureName NVARCHAR(100)); INSERT INTO @ProcedureList (ProcedureName) VALUES ('spAATcao'), ('spAnotherProcedure'), ('spThirdProcedure'); -- Variables for execution DECLARE @ProcName NVARCHAR(100); DECLARE @SQL NVARCHAR(MAX); DECLARE @Parameter INT = 11223; -- Input parameter for procedures -- Cursor to loop through each procedure DECLARE ProcCursor CURSOR FOR SELECT ProcedureName FROM @ProcedureList; OPEN ProcCursor; FETCH NEXT FROM ProcCursor INTO @ProcName; WHILE @@FETCH_STATUS = 0 BEGIN -- Construct dynamic SQL to execute the procedure and capture the result set SET @SQL = N'INSERT INTO ##Results (ProcedureID, Total) EXEC ' + @ProcName + ' @rm = @Parameter;'; -- Execute the dynamic SQL and insert the results into the ##Results table EXEC sp_executesql @SQL, N'@Parameter INT', @Parameter = @Parameter; FETCH NEXT FROM ProcCursor INTO @ProcName; END; CLOSE ProcCursor; DEALLOCATE ProcCursor; -- Display the results from the global temporary table SELECT * FROM ##Results; -- Clean up the global temporary table DROP TABLE ##Results;
Leave a Comment