I have to insert execution of stored procedure into table. For example, I have procedure called proc
and I want execute it and insert its data into table. I found some example like this:
DECLARE @storedProcname NVARCHAR(MAX) = ''
SET @storedProcname = 'proc'
DECLARE @strSQL AS VARCHAR(MAX) = 'CREATE TABLE myTableName '
SELECT @strSQL = @strSQL+STUFF((
SELECT ',' + name +' ' + system_type_name
FROM sys.dm_exec_describe_first_result_set_for_object (OBJECT_ID(@storedProcname),0)
FOR XML PATH('')),1,1,'(') + ')'
print @strSQL
EXEC (@strSQL)
INSERT INTO myTableName
EXEC ('proc')
SELECT * FROM myTableName
--DROP TABLE myTableName
It works for THIS procedure but how to create for each procedure its own table for inserting data? Because I have a lot of procedures and they all have different amount of columns and datatype of columns is different too. I imagine I'll write some cursor, where the script will list all of procedures, create tables for them and insert them into tables or something else.