I have a SelectSP which returns resultset that i am storing in the temp table. Now i want to call another SP named InsertSP and pass the values from this temp table as parameter to the it.
Here is the code i have so far:
CREATE TABLE #tempTable
(
Field1 VARCHAR(255),
Field2 VARCHAR(255),
Field3 VARCHAR(255),
Field4 VARCHAR(255),
Field5 VARCHAR(255),
Field6 VARCHAR(255),
Field7 VARCHAR(255)
.
.
.
)
INSERT INTO #tempTable exec
usp_SelectSP 'p1' ,'p2', 'p3'
Now i want to execute one more SP and pass Field1, Field2, Field3 of tempTable as parameter to that SP.
I was thinking something like this
usp_InsertSP
@param1 = Field1
@Param2 = Field2
@Param3 = Field3
FROM #tempTable.
What is the best way to do this?
If you can’t change the called SP then easiest way would probably be a cursor:
However I strongly recommend you change the called procedure. There is almost absolutely a more efficient way to deal with the data in this #temp table if you can change the called procedure to use it directly.
EDIT
Demonstrating that a called proc can see a #temp table created by the caller.
Results: