Dynamic Pivot result into fixed table (SSIS or SQL)
I got a stored procedure, which gives me data by a dynamic pivot a开发者_JAVA技巧nd I want to insert this data into a table, which has all possible columns, the dynamic pivot could produce.
Example:
The dynamic pivot stored procedure gives the following table as result:
Property_A Property_C
----------------------
Value_A Value_A
Value_B Value_C
...
On another run the same SP may produce
Property_A Property_B
----------------------
Value_D Value_A
...
Whatever it returns, the result should be put into a table like this
Property_A Property_B Property_C
----------------------------------
Value_A NULL Value_A
Value_B NULL Value_C
Value_D Value_A NULL
and so on.
How can I achieve this via SQL (MS SQL Server) or better via SSIS?
Thanks in advance,
tombom
This is a bit tricky to handle if the calling process doesn't know what the columns are in the results. To insert into a table (permanent or temporary), the number of columns inserted must match the number of columns to insert.
Here's how you would insert the results of the SP to a table. Notice that you don't have to include all columns in the table. However, in my example, usp_MakeDate_1 returns two of three columns (Property_A, Property_C) and usp_MakeData_2 returns a set of two different columns (Property_A, Property_B).
INSERT INTO MyResults (Property_A, Property_C)
EXEC dbo.usp_MakeData_1
INSERT INTO MyResults (Property_A, Property_B)
EXEC dbo.usp_MakeData_2
You'll find that SSIS is even more restrictive when it comes to dynamic SQL. For the most part, your outputs and inputs must be known at design time.
SELECT
*
INTO
MyNewTable
FROM
TempTableProducedByStoredProc
精彩评论