I have data in a couple tables that I want to load into a temporary table in SQL Server.
I am creating a stored procedure that gets the data from the tables. Then I create the global temporary table and insert the results from the stored procedure into it.
However when trying to select the data from the temporary table I get this error 'Invalid object name ##Temp'
Why am I getting this error if it is a global temporary table?
DROP PROCEDURE usp_GetEmp
GO
CREATE PROCEDURE usp_GetEmp
AS
BEGIN
SELECT table1.id AS Id
, table2.data AS table2_value
FROM table1 INNER JOIN table2
ON table1.id = table2.table1_id
WHERE table2.data = 1
END
CREATE TABLE ##Temp
( Id Int
, Value varchar(50))
INSERT INTO ##Temp
EXEC usp_GetEmp
GO
SELECT *
FROM ##Temp
Create TableandInsert Intostatement because of where yourGOstatement is. Credit to @Fabiano below. I didn't see his answer until now. - SS_DBA