简体   繁体   English

将动态数据透视表结果转换为固定表(SSIS或SQL)

[英]Dynamic Pivot result into fixed table (SSIS or SQL)

I got a stored procedure, which gives me data by a dynamic pivot and 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 在另一轮运行中,相同的SP可能会产生

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? 如何通过SQL(MS SQL Server)或通过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. 这是将SP的结果插入表中的方法。 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). 但是,在我的示例中,usp_MakeDate_1返回三个列中的两个(Property_A,Property_C),usp_MakeData_2返回一组两个不同的列(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. 您会发现SSIS在动态SQL方面的限制更大。 For the most part, your outputs and inputs must be known at design time. 在大多数情况下,必须在设计时就知道您的输出和输入。

SELECT
  *
INTO
  MyNewTable
FROM
  TempTableProducedByStoredProc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM