English 中文(简体)
储存程序表,可变数栏目
原标题:Stored Procedure to a Table with Variable Number of columns

当结果能够有数量可变的栏目时,你如何将特殊产品的产出列入表格?

本次级工作方案列出了各栏数可能各不相同的表格。 我需要把结果列入一个温和表,以便我能够用它来盘问报告。 我如何建立一个能够处理SP产出的表格?

 DECLARE @cols AS VARCHAR(MAX),
 @query  AS VARCHAR(MAX); 

 SET @cols = STUFF((SELECT distinct  ,  + QUOTENAME(c.question) 
        FROM temp_SURVEY_DATA c
        FOR XML PATH(  ), TYPE
        ).value( . ,  VARCHAR(MAX) ) ,1,1,  )

 set @query =   SELECT responseid, moduleid, responsedatetime,   + @cols +   from 
        (select question, response, responsedatetime, responseid, moduleid
            from temp_SURVEY_DATA) x
        pivot 
        ( max(response)
            for question IN(  + @cols +  ) ) p  

          execute(@query)
问题回答

a. 专门为此类物品使用静态表格,并在表格中添加结果,而不是将浏览量退回给电线人:

DECLARE @cols AS VARCHAR(MAX),
        @query  AS VARCHAR(MAX); 

 SET @cols = STUFF((SELECT distinct  ,  + QUOTENAME(c.question) 
        FROM temp_SURVEY_DATA c
        FOR XML PATH(  ), TYPE
        ).value( . ,  VARCHAR(MAX) ) ,1,1,  );

EXECUTE ( IF OBJECT_ID(  temp_SURVEY_DATA_pivoted  ) IS NOT NULL
  DROP TABLE temp_SURVEY_DATA_pivoted );

set @query =   SELECT responseid, moduleid, responsedatetime,   + @cols +  
  INTO temp_SURVEY_DATA_pivoted
  from 
        (select question, response, responsedatetime, responseid, moduleid
            from temp_SURVEY_DATA) x
        pivot 
        ( max(response)
            for question IN(  + @cols +  ) ) p  

          execute(@query)

或者在显示数据之前,在其他地方使用动态文字时放弃这种说法。





相关问题
SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management ...

Most efficient way to store number 11.111 in SQL Server

What datatype is the most efficient to store a number such as 11.111. The numbers will have up 2 digits before the point and up to three after. Currently the column is a bigint , I am guessing that ...

表格和数据表

是否需要在提瓜表之后更新表格统计数据,还是自动更新?

Inconsistent Generate Change Script

I add a column of type tinyint and being set to not allow nulls in a table and generate the change scripts. The table has data in it at this time. The script has code that creates a temp table and ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

selecting one value out of an xml column

I have a particularly troublesome xml column to query data from. The schema is fixed by the Quebec Ministry of Revenue so "it is what it is" The important part of the query looks like this: with ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签