动态数据透视表语法错误

时间:2015-06-26 19:13:34

标签: sql-server syntax-error pivot-table

我收到错误讯息 - 当我运行以下查询时Msg 102, Level 15, State 1, Line 5 Incorrect syntax near ','.。我无法弄清楚为什么语法不正确。有没有人有什么建议?

DECLARE @Vendor AS INT = 41

CREATE TABLE #PivotData
(
    [ID] INT,
[Row] VARCHAR(MAX),
[Columns] VARCHAR(MAX),
[AggData] INT
)

INSERT INTO #PivotData

SELECT  V.Vendor_Key
    ,O.Location_Desc 
    ,P.Item_Desc
    ,IIF.Sales_Qty
FROM    PDI.PDI_Warehouse_952_01.dbo.Item_Inventory_Fact IIF
    INNER JOIN  PDI.PDI_Warehouse_952_01.dbo.Item_Purchases_Fact IPF
    ON IIF.Calendar_Key = IPF.Calendar_Key
    AND IIF.Organization_Key = IPF.Organization_Key
    AND IIF.Product_Key = IPF.Product_Key
INNER JOIN  PDI.PDI_Warehouse_952_01.dbo.Organization O
    ON IIF.Organization_Key = O.Organization_Key
INNER JOIN  PDI.PDI_Warehouse_952_01.dbo.Product P
    ON IIF.Product_Key = P.Product_Key
INNER JOIN PDI.PDI_Warehouse_952_01.dbo.Vendor V
    ON IPF.Vendor_Key = V.Vendor_Key
WHERE V.Vendor_key = @Vendor

DECLARE @Cols AS NVARCHAR(MAX),
    @Query AS NVARCHAR(MAX)

SET @Cols = STUFF ((SELECT DISTINCT ',' + QUOTENAME(P.Columns)
                FROM #PivotData P
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)')
                ,1,1,'')

 SET @Query = 'SELECT Row, ' + @Cols + ' FROM
            (
                SELECT  Row
                        ,Column
                        ,Aggregate
                FROM #PivotData
            )x
            PIVOT
            (
                SUM(Aggregate)
                FOR Row IN (' + @Cols + ')
            ) p '

EXECUTE(@Query)

DROP TABLE #PivotData

4 个答案:

答案 0 :(得分:1)

错误消息不是很有用,但如果查看生成的查询及其中的第五行,您会看到错误发生,因为您尝试使用不正确的名称从列中进行选择。

使用列名[Row], [Columns], [AggData]

创建临时表

但是当您从中选择并转动时,请使用这些列名称Row, Column, Aggregate

这不会很好。解决方案是在@query中使用正确的列名。

答案 1 :(得分:0)

语句之间的分号丢失了吗?

例如,在create tableinsert into ......

之间

答案 2 :(得分:0)

首先,我建议为变量声明默认的空值,然后以不同的方式连接行。

DECLARE @Cols AS NVARCHAR(MAX) = ''
    , @Query AS NVARCHAR(MAX) = '';

SELECT @Cols += N',' + QUOTENAME(P.Columns)
FROM (
    SELECT DISTINCT P.Columns
    FROM #PivotData  AS P
    ) AS P;

-- Generated list should like this:
-- ,[column1],[column2],[column3]...[columnN]
-- Only first comma has to be removed in PIVOT
SET @Query += N'SELECT P.Row ' + @Cols + '
                FROM (
                    SELECT Row, Column, Aggregate
                    FROM #PivotData
                    ) AS S
                    PIVOT (
                        SUM(Aggregate)
                        FOR Row IN (' + STUFF(@Cols, 1, 1, '') + ')
                    ) AS P';

--PRINT @Query;
EXECUTE @Query;

这应该有用。

答案 3 :(得分:0)

我很确定您只是将列字符串定义错误。我没有测试过,只是看了它。但是您的列字符串类似于,[1],[2]。 一开始,是错误的。试试这个:

DECLARE @Vendor AS INT = 41

CREATE TABLE #PivotData
(
    [ID] INT,
[Row] VARCHAR(MAX),
[Columns] VARCHAR(MAX),
[AggData] INT
)

INSERT INTO #PivotData

SELECT  V.Vendor_Key
    ,O.Location_Desc 
    ,P.Item_Desc
    ,IIF.Sales_Qty
FROM    PDI.PDI_Warehouse_952_01.dbo.Item_Inventory_Fact IIF
    INNER JOIN  PDI.PDI_Warehouse_952_01.dbo.Item_Purchases_Fact IPF
    ON IIF.Calendar_Key = IPF.Calendar_Key
    AND IIF.Organization_Key = IPF.Organization_Key
    AND IIF.Product_Key = IPF.Product_Key
INNER JOIN  PDI.PDI_Warehouse_952_01.dbo.Organization O
    ON IIF.Organization_Key = O.Organization_Key
INNER JOIN  PDI.PDI_Warehouse_952_01.dbo.Product P
    ON IIF.Product_Key = P.Product_Key
INNER JOIN PDI.PDI_Warehouse_952_01.dbo.Vendor V
    ON IPF.Vendor_Key = V.Vendor_Key
WHERE V.Vendor_key = @Vendor

DECLARE @Cols AS NVARCHAR(MAX),
    @Query AS NVARCHAR(MAX)

SELECT @Cols = COALESCE(@Cols + N',['+P.Columns+N']',
                    N'['+P.Columns+']')
FROM (SELECT DISTINCT Columns FROM #PivotData) P

 SET @Query = 'SELECT Row, ' + @Cols + ' FROM
            (
                SELECT  Row
                        ,Column
                        ,Aggregate
                FROM #PivotData
            )x
            PIVOT
            (
                SUM(Aggregate)
                FOR Row IN (' + @Cols + ')
            ) p '

EXECUTE(@Query)

DROP TABLE #PivotData