简体   繁体   English

如何将动态数据透视输出结果插入SQL服务器的临时表?

[英]How to insert dynamic pivot output result into temp table in SQL server?

I have tried all below links ,But they are not working in my Sever SQL? 我已经尝试了以下所有链接,但他们不在我的Sever SQL中工作?

SET @cols = STUFF((SELECT ',' + QUOTENAME(AD.MonthFormat) FROM #tempMonthFormat AD FOR xml PATH (''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
  SELECT @Selectcols =  STUFF((SELECT 
    ',' + ISNULL(@Selectcols + ',','')+ 'ISNULL(' + QUOTENAME(AD.MonthFormat) + ', 0) AS ' + QUOTENAME(AD.MonthFormat)  FROM #tempMonthFormat AD 
  FOR xml PATH (''), TYPE)
  .value('.', 'nvarchar(max)'), 1, 1, '');

Declare @FINALQUERY varchar(max);    
SET @FINALQUERY = '         
                      SELECT  Name,[Issue],' + @Selectcols + ' FROM
                     (
                         SELECT   into #temptable * FROM #Meantimeresult  
                     ) Y
                     PIVOT 
                     (
                         SUM(MT)
                         FOR [Monthformat] IN (' + @cols + ')               
                    ) p GROUP BY ' + @cols+',[Issue], Name'                

        EXEC SP_EXECUTESQL @FINALQUERY;

Dynamic SQL Result INTO #Temp Table 动态SQL结果INTO #Temp表

How to create temp table from a dynamic query? 如何从动态查询创建临时表?

INSERT INTO temp table from unknown number and name of columns (from dynamic PIVOT query) Dynamic Pivot Results to a Temp Table 从未知数量和列名称(从动态PIVOT查询)INSERT INTO临时表 动态数据透视表结果到临时表

Can be done with a couple of steps, but the convoluted nature of this answer should indicate to you that you are fighting against sql server here, not with it. 可以通过几个步骤完成,但这个答案的复杂性应该向您表明您在这里与sql服务器作斗争,而不是与它作斗争。 Try very hard to find another way to structure your reports so that this is not necessary before undertaking such drastic measures as these. 努力寻找另一种方法来构建报告,以便在采取如此严厉的措施之前不必这样做。

1) Encapsulate / hack your initial pivot query as a parameterless stored procedure. 1)将初始数据透视查询封装/破解为无参数存储过程。 If you need to pass any data into the procedure you must structure it as a global temp table (## prefixed temp table) eg 如果需要将任何数据传递到过程中,则必须将其构造为全局临时表(## prefixed temp table),例如

CREATE PROCEDURE dynamic_meantime_pivot AS
DECLARE 
    @cols nvarchar(max) = STUFF(
        (SELECT ',' + QUOTENAME(AD.MonthFormat) FROM ##tempMonthFormat AD FOR xml PATH (''), TYPE)
        .value('.', 'nvarchar(max)'), 1, 1, '')
    @Selectcols nvarchar(max) = STUFF(
        (SELECT ',' + ISNULL(@Selectcols + ',','')+ 'ISNULL(' + QUOTENAME(AD.MonthFormat) + ', 0) AS ' + QUOTENAME(AD.MonthFormat)  FROM ##tempMonthFormat AD FOR xml PATH (''), TYPE)
        .value('.', 'nvarchar(max)'), 1, 1, '');

Declare @FINALQUERY varchar(max) = '         
                      SELECT  Name,[Issue],' + @Selectcols + ' FROM
                     (
                         SELECT * FROM ##Meantimeresult  
                     ) Y
                     PIVOT 
                     (
                         SUM(MT)
                         FOR [Monthformat] IN (' + @cols + ')               
                    ) p GROUP BY ' + @cols+',[Issue], Name'                

EXEC(@FINALQUERY)

2) Set up any global temp tables required by the above procedure. 2)设置上述过程所需的任何全局临时表。

3) Call the procedure via the OPENROWSET function as below: 3)通过OPENROWSET函数调用该过程,如下所示:

SELECT * INTO #dynamic_meantime_table FROM OPENROWSET('SQLNCLI', 'Server=<your_sql_server_instance_name_here>;Trusted_Connection=yes;',
     'EXEC dynamic_meantime_pivot')

See also this excellent answer regarding OPENROWSET. 另请参阅关于OPENROWSET的这个优秀答案。

Insert results of a stored procedure into a temporary table 将存储过程的结果插入临时表

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

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