There is a method for creating a dummy temp table with a single identifier column, and then modifying this table with the required schema through dynamic SQL and populating it. Thus, you can use the temporary table in both dynamic and regular SQL, join it ...
-- Create dummy table CREATE TABLE #tmpContactData (PK int NOT NULL IDENTITY(1,1)) -- Alter its schema DECLARE @sqlCommand nvarchar(max) SELECT @sqlCommand = ' ALTER TABLE #tmpContactData ADD EmployeeId int, Address varchar(100), Phone varchar(50) ' EXECUTE(@sqlCommand) -- Fill it SELECT @sqlCommand = ' INSERT INTO #tmpContactData SELECT t.EmployeeId, t.Address, t.Phone FROM ( SELECT EmployeeId=1000, Address=''Address 1000'', Phone=''Phone 1000'' UNION SELECT 1001, ''Address 1001'', ''Phone 1001'' UNION SELECT 1002, ''Address 1002'', ''Phone 1002'' ) t ' EXECUTE(@sqlCommand) --select from it SELECT * FROM #tmpContactData --CleanUp DROP TABLE #tmpContactData
Stanko ogrin
source share