Prøv at bruge lokal temp-tabel -
IF OBJECT_ID ('tempdb.dbo.#temp') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp (ID INT)
INSERT INTO #temp (ID)
VALUES (1),(2)
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = 'SELECT * FROM #temp'
EXEC sys.sp_executesql @SQL