简体   繁体   English

动态数据透视表错误

[英]Dynamic Pivot table error

I am currently having trouble getting the following to work. 我目前无法使以下各项正常工作。 I have a table, 'tbl_View', that exists in my database that has the column names of another table held in its rows (users are able to choose specific columns that they wish to display and their list of their choices is kept here). 我的数据库中有一个表'tbl_View',该表的行中保存着另一个表的列名(用户可以选择希望显示的特定列,并且将选择的列表保留在此处)。

I then want to create a view that will bring only each users specific column choices from the target table 'AREG' and would like to do so dynamically if possible. 然后,我想创建一个视图,该视图将仅从目标表“ AREG”中为每个用户带来特定的列选择,并且希望在可能的情况下动态地这样做。

Table Samples - AREG Column names: 表样本-AREG列名称:

ID | ID | Text_1 | 文字_1 | Text_2 | 文字_2 | Text_3 | Text_3 | Text_4 etc... Text_4等...

tbl_View Data: tbl_查看数据:

ViewID | ViewID | lblValue | lblValue | fldName | fldName | showYN | showYN | sOrder 订单

10 | 10 | EANPR | EANPR | YesNo8 | 是否8 | True | 是的 80 80

10 | 10 | INSP_DATE| INSP_DATE | Date_1 | 日期_1 | True | 是的 81 81

10 | 10 | KG_DATE | KG_DATE | Date_2 | 日期_2 | True | 是的 82 82

11 | 11 | Life | 生活| Num_13 | Num_13 | True | 是的 35 35

11 | 11 | Area | 面积 Dimension_1 | Dimension_1 | True | 是的 69 69

11 | 11 | Length | 长度 Dimension_2 | Dimension_2 | True | 是的 70 70

11 | 11 | EANPR | EANPR | YesNo8 | 是的否8 | True | 是的 80 80

12 | 12 | TRAVEL | 旅行| Text_1 | 文字_1 | True | 是的 1 1个

12 | 12 | SPILLLVE | 溢价| Text_2 | 文字_2 | True | 是的 2 2

12 | 12 | SLOPE_PCT| SLOPE_PCT | Text_3 | Text_3 | True | 是的 3 3

14 |Project_Name | 14 |项目名称| Text_1 | 文字_1 | True | 是的 1 1个

14 | 14 | Project Description | 项目说明 Text_2 | 文字_2 | 2 2

code is: 代码是:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(fldName)
FROM (SELECT DISTINCT fldName FROM qry_ADet WHERE (Viewid=14)) AS fname 

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = N'SELECT ' + @ColumnName + ' FROM #AREG   PIVOT ' + @ColumnName + ' AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

I keep getting the error: 我不断收到错误:

Msg 102, Level 15, State 1, Line 1 Msg 102,第15级,状态1,第1行

Incorrect syntax near 'Text_1'. 'Text_1'附近的语法不正确。

I have hard coded the view id into the query for this example but will use a parameter in my final query as it will be referenced by an asp.net datasource. 我已将视图ID硬编码到此示例的查询中,但是将在最终查询中使用参数,因为它将由asp.net数据源引用。 But the error points to a different column name if you change the Viewid clause. 但是,如果您更改Viewid子句,则错误将指向不同的列名。

Please help. 请帮忙。

Thanks 谢谢

too long for comment, I think the issue may stem from the column concatenation and it needs something like this: 评论太久了,我认为问题可能源于列串联,它需要这样的内容:

SET @ColumnName = STUFF((SELECT distinct ',' + QUOTENAME(fldName) 
            FROM qry_ADet
             WHERE (Viewid=14)
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

to debug a dynamic sql query, do a PRINT statement before executing it. 要调试动态sql查询,请在执行它之前执行PRINT语句。

You will probably see that the error is from @DynamicPivotQuery - you should be selecting from tbl_View instead of #AREG right ? 您可能会看到错误来自@DynamicPivotQuery-您应该从tbl_View中选择而不是#AREG对吗? - your pivot statement is wrong. -您的枢纽声明是错误的。 it should be something like 它应该像

FROM sometable
PIVOT
(
     sum( somecol)
     for anothercol in ( [value1], [value2] , [value3] )
) p

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

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