I would like to set up a table within a SQL Server DB that stores the results from a long and complex query that takes almost an hour to run. After running the query the rest of the analysis is done by colleagues using Excel pivot tables.
I would prefer not to output the results to text, and want to keep it within SQL Server and then just set up Excel to pivot directly from the server.
My problem is that the output will not always have exactly the same columns, and manually setting up an output table to INSERT INTO every time would be tedious.
Is there a way to create a table on the fly based on the type of data you are selecting?
E.g. if I want to run:
SELECT
someInt,
someVarchar,
someDate
FROM someTable
And insert this into a table called OutputTable, which has to look like this
CREATE TABLE OutputTable
(
someInt int null
someVarchar varchar(255) null,
someDate date null,
) ON [primary]
Is there some way to make SQL Server interrogate the fields in the select statement and then automatically generate the CREATE TABLE script?
Thanks
Karl
…doesn’t explicitly generate a CREATE script (at least not one you can see) but does the job!