I'm defining a long dynamic query and I'd like to insert it's results into a table. However, I'd prefer not to define the table first. Is this possible?
The query works correctly, I see the expected results if I run this:
declare @query VARCHAR(MAX)
@query = 'SELECT
--a bunch of stuff involving joins and pivots and such
'
execute (@query)
But neither of these attempts to select into an un-defined temp table work:
--attempt 1
declare @query VARCHAR(MAX)
@query = 'SELECT * INTO #T1 (
SELECT
--a bunch of stuff involving joins and pivots and such
)
'
execute (@query)
--attempt 2
declare @query VARCHAR(MAX)
@query = 'SELECT
--a bunch of stuff involving joins and pivots and such
'
execute (@query)
select * INTO #T1 execute (@query)