2016-08-22 84 views
0

我得到了非常簡單的查詢,但我無法使其工作。 請幫忙。謝謝動態樞軸Sql服務器

我跟着嘖嘖,但我的查詢不起作用

DECLARE @ColumnName NVARCHAR(MAX) = '' 
DECLARE @Query NVARCHAR(MAX) = '' 
DECLARE @Site NVARCHAR(MAX) = '' 
DECLARE @Date NVARCHAR(MAX) = '' 
DECLARE @MOnth NVARCHAR(MAX) = '' 


SELECT @ColumnName += QUOTENAME([Product Category]) + ',' 
FROM 
(
    SELECT DISTINCT [Product Category] 
    FROM vw_TTMTALK_BREAKDOWN_DETAIL_LINE 
    WHERE [Customer No] = 'SLPIP' AND 
    DATEPART(yyyy, [Posting Date])= '2016' AND 
    CONVERT(CHAR(3), [Posting Date], 0)= 'Jan' 
    GROUP BY [Customer No], [Product Category] 
) AS T1 

SET @ColumnName = LEFT(@ColumnName,LEN(@ColumnName)-1) 

set @Site = 'SLPIP' 
set @Date = '2016' 
set @Month = 'Jan' 

SET @Query = 'SELECT * FROM 
(
SELECT [Customer No], [Product Category] 
FROM vw_TTMTALK_BREAKDOWN_DETAIL_LINE 
WHERE [Customer No] = ' + @Site + ' AND 
DATEPART(yyyy, [Posting Date])=' + @Date + ' AND 
CONVERT(CHAR(3), [Posting Date], 0)=' + @Month + ' 
--GROUP BY [Customer No], [Product Category] 
) T2 
PIVOT (
    COUNT([Customer No]) 
    FOR [Product Category] IN (' + @ColumnName + ') 
) T3' 
--print @Query 
EXEC sp_executesql @Query 

錯誤

Msg 207, Level 16, State 1, Line 5 
Invalid column name 'SLPIP'. 
Msg 207, Level 16, State 1, Line 7 
Invalid column name 'Jan'. 
+2

動態查詢中缺少@site周圍的標記。和大約一個月。假定兩者都是字符串數據類型。在網站前後添加char(39)。例如:'WHERE [Customer No] ='+ char(39)+ @ Site + char(39)'AND' – xQbert

+0

謝謝您的主席xQbert您的解決方案的工作 –

回答

2

首先,嘗試儘可能地使用參數化查詢,並且僅在絕對必要時使用字符串連接。

所以通過固定的是,您的查詢應該以某種方式那樣:

DECLARE @ColumnName NVARCHAR(MAX) = ''; 
DECLARE @Query NVARCHAR(MAX) = ''; 
DECLARE @Site NVARCHAR(MAX) = ''; 
DECLARE @Date NVARCHAR(MAX) = ''; 
DECLARE @MOnth NVARCHAR(MAX) = ''; 

SELECT @ColumnName += ',' + QUOTENAME([Product Category]) 
FROM (
    SELECT DISTINCT [Product Category] 
    FROM vw_TTMTALK_BREAKDOWN_DETAIL_LINE 
    WHERE [Customer No] = 'SLPIP' 
     AND DATEPART(yyyy, [Posting Date]) = '2016' 
     AND CONVERT(CHAR(3), [Posting Date], 0) = 'Jan' 
    GROUP BY [Customer No], [Product Category] 
    ) AS T1; 

SET @Site = 'SLPIP'; 
SET @Date = '2016'; 
SET @Month = 'Jan'; 

SET @Query = ' 
    SELECT * 
    FROM (
     SELECT [Customer No], [Product Category] 
     FROM vw_TTMTALK_BREAKDOWN_DETAIL_LINE 
     WHERE [Customer No] = @Site 
      AND DATEPART(yyyy, [Posting Date]) = @Date 
      AND CONVERT(CHAR(3), [Posting Date], 0) = @Month 
     ) T2 
    PIVOT (
     COUNT([Customer No]) 
     FOR [Product Category] IN (' + STUFF(@ColumnName, 1, 1, '') + ') 
     ) T3'; 

EXEC sp_executesql @Query 
    , N'@Site NVARCHAR(MAX), @Date NVARCHAR(MAX), @Month NVARCHAR(MAX)' 
    , @Site 
    , @Date 
    , @Month; 

我也鼓勵您解決您查詢的方式[發佈日期]

取而代之的是:

AND DATEPART(yyyy, [Posting Date]) = '2016' 
AND CONVERT(CHAR(3), [Posting Date], 0) = 'Jan' 

我把它改寫這樣的:

AND [Posting Date] >= '2016-01-01' 
AND [Posting Date] < '2016-02-01'; 

這將使用[Posting Date]上的索引,如果有的話。

+0

第一次運行它不起作用,5分鐘後它工作, 先生非常感謝您。實際上我在查詢中使用Year。我濫用變量@ –

2

你需要,當你建立你的查詢,因爲這些值是使用以防萬一SLPIP和Jan報價字符串