I would like to select into table variable from @query
.
Here is @query
:
SELECT @QUERY = 'SELECT DISTINCT USER_KEY, (SELECT TOP(1) USER_FNM FROM BAUSER WHERE BAUSER.USER_KEY = PIVOT_LOCATIONS.USER_KEY), ' + @COLDEPSUMMARY + '
FROM CAUSDE_TAS
PIVOT
(
SUM(USDE_HSU)
FOR DEPA_KEY IN (' + @COLDEPARTMENTS + ')
) PIVOT_LOCATIONS
WHERE USDE_DAT >= ''' + format(@DATEFROM, 'MM.dd.yyyy') + ''' AND USDE_DAT <= ''' + format(@DATETO, 'MM.dd.yyyy') + '''
AND USER_KEY IN (' + @USERS_STR + ')
GROUP BY USER_KEY'
EXECUTE (@QUERY)
What I would like to do is get results from EXECUTE(@QUERY)
and insert those results into temporary @USERS
table:
DECLARE @USERS TABLE
(
USER_KEY INT,
USER_FNM VARCHAR(50),
USDE_HSU DECIMAL(8,2),
[110000003] DECIMAL(8,2),
[120000002] DECIMAL(8,2),
[120000003] DECIMAL(8,2),
[120000004] DECIMAL(8,2),
[120000005] DECIMAL(8,2),
[120000006] DECIMAL(8,2),
[120000007] DECIMAL(8,2),
[120000008] DECIMAL(8,2)
)
Is this possible?