Quantcast
Viewing all articles
Browse latest Browse all 12198

Temp tables created in an EXEC

Note that temporary tables created in an EXEC statement ie.EXEC ('SELECT ID, Name INTO #TEMP FROM Customers')SELECT * FROM #TEMP -- This will fail because #TEMP is not a valid objectare out of scope when the EXEC returns and cannot be accessed by the SQL that made the EXEC call. The solution is to prebuild the temp table ie.CREATE TABLE #TEMP (ID as int, Name as varchar(255))EXEC ('INSERT INTO #TEMP(ID, Name) SELECT ID, Name FROM Customers')SELECT * FROM #TEMP -- This works

Viewing all articles
Browse latest Browse all 12198

Trending Articles