Reputation: 117
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
Upvotes: 2
Views: 7821
Reputation: 512
Try it's
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
GO
IF OBJECT_ID('TEMPDB.DBO.##TEMP') IS NOT NULL
DROP TABLE ##TEMP
GO
CREATE TABLE ##TEMP
( ID INT
, VALUE VARCHAR(50))
INSERT INTO ##TEMP
EXEC USP_GETEMP
GO
SELECT *
FROM ##TEMP
Upvotes: 1