How to insert into a temp table that is all ready created inside of a stored procedure
ALTER PROCEDURE [dbo].[Report_1]
BEGIN
CREATE TABLE #Temp
(
col1 INT,
col2 INT,
col3 VARCHAR(50)
)
INSERT INTO #Temp
EXEC [spSelection] @ID
..do stuff
..do stuff
..do stuff
SELECT * FROM #temp
END
The problem I am having is, I will use this stored procedure (spSelection
) in the future and if I change this stored procedure to get more columns for a different stored procedure, then Report_1
will fail.
So I need a way to dynamically create the table or be able to only select distinct columns from the output of exec [spSelection] @ID
or have Report_1
be able to read from a temp table created in spSelection
.
I have tried to use a global and that will not work because it can be used by other stored procedure at the same time, if I create a dynamic SQL.
@sql ='
create table #Temp(
col1 int,col2 int,col3 varchar(50)
) ' exec sp_executesql @sql
I can not access the #temp
table outside of the quotes