2014-09-05 481 views
0

目前我有數據,這種格式SQL Server的PIVOT查詢 「轉換失敗」

Current Format

而且我想以這種形式

Account AccountUnit Description ATL BOS 
1111  10   Trucking 23 50  
2222  13   Banking 34 21  

我有我的查詢如下

DECLARE 
@FISCAL_YEAR AS NVARCHAR(4), 
@PERIOD AS VARCHAR(2), 
@COL AS NVARCHAR(MAX), 
@SQL AS NVARCHAR(MAX) 

SELECT @COL= COALESCE (@COL+ ',','')+QUOTENAME(MX_VALUE)FROM 
(SELECT DISTINCT MX_VALUE FROM AUMXVALUE WHERE MATRIX_CAT='_DIV') AS A 

SET @FISCAL_YEAR = 2014 
SET @PERIOD=6 


SET @SQL= 
N' 
SELECT CURR.COMPANY, CURR.ACCT_UNIT, CURR.DESCRIPTION, CURR.ACCOUNT, 
(
(SELECT UNITS.* , GLN.DESCRIPTION FROM 
(SELECT 
GLU.COMPANY, 
GLU.ACCT_UNIT, 
GLU.ACCOUNT, 
AMV.MATRIX_CAT, 
'[email protected]+', 
CASE 
WHEN @PERIOD=1 THEN GLU.DB_UNITS_01+GLU.CR_UNITS_01 
WHEN @PERIOD=2 THEN GLU.DB_UNITS_02+GLU.CR_UNITS_02 
WHEN @PERIOD=3 THEN GLU.DB_UNITS_03+GLU.CR_UNITS_03 
WHEN @PERIOD=4 THEN GLU.DB_UNITS_04+GLU.CR_UNITS_04 
WHEN @PERIOD=5 THEN GLU.DB_UNITS_05+GLU.CR_UNITS_05 
WHEN @PERIOD=6 THEN GLU.DB_UNITS_06+GLU.CR_UNITS_06 
WHEN @PERIOD=7 THEN GLU.DB_UNITS_07+GLU.CR_UNITS_07 
WHEN @PERIOD=8 THEN GLU.DB_UNITS_08+GLU.CR_UNITS_08 
WHEN @PERIOD=9 THEN GLU.DB_UNITS_09+GLU.CR_UNITS_09 
WHEN @PERIOD=10 THEN GLU.DB_UNITS_10+GLU.CR_UNITS_10 
WHEN @PERIOD=11 THEN GLU.DB_UNITS_11+GLU.CR_UNITS_11 
WHEN @PERIOD=12 THEN GLU.DB_UNITS_12+GLU.CR_UNITS_12 
ELSE 0 END AS ACT 
FROM GLUNITS GLU JOIN GLNAMES GLN 
ON GLU.COMPANY=GLN.COMPANY 
AND GLU.ACCT_UNIT='''+100+''' 
JOIN AUMXVALUE AMV 
ON GLN.OBJ_ID=AMV.OBJ_ID 
AND AMV.MATRIX_CAT='''+'_DIV'+''' 
AND [email protected]_YEAR 
AND GLU.ACCOUNT=9100 
)UNITS 
JOIN GLNAMES GLN 
ON UNITS.COMPANY=GLN.COMPANY 
AND UNITS.ACCT_UNIT=GLN.ACCT_UNIT) CURR 
) T PIVOT(SUM(ACT) FOR MX_VALUE IN (' + @COL + ')) AS PIVTBL' 


EXEC (@SQL) 

,但我不斷收到此錯誤

Conversion failed when converting the nvarchar value ' 

SELECT CURR.COMPANY, CURR.ACCT_UNIT, CURR.DESCRIPTION, CURR.ACCOUNT, 
(
(SELECT UNITS.* , GLN.DESCRIPTION FROM 
(SELECT 
GLU.COMPANY, 
GLU.ACCT_UNIT, 
GLU.ACCOUNT, 
AMV.MATRIX_CAT, 
[ATL        ],[BOS        ] 
CASE 
    WHEN @PERIOD=1 THEN GLU.DB_UNITS_01+GLU.CR_UNITS_01 
    WHEN @PERIOD=2 THEN GLU.DB_UNITS_02+GLU.CR_UNITS_02 
    WHEN @PERIOD=3 THEN GLU.DB_UNITS_03+GLU.CR_UNITS_03 
WHEN @PERIOD=4 THEN GLU.DB_UNITS_04+GLU.CR_UNITS_04 
WHEN @PERIOD=5 THEN GLU.DB_UNITS_05+GLU.CR_UNITS_05 
WHEN @PERIOD=6 THEN GLU.DB_UNITS_06+GLU.CR_UNITS_06 
WHEN @PERIOD=7 THEN GLU.DB_UNITS_07+GLU.CR_UNITS_07 
WHEN @PERIOD=8 THEN GLU.DB_UNITS_08+GLU.CR_UNITS_08 
WHEN @PERIOD=9 THEN GLU.DB_UNITS_09+GLU.CR_UNITS_09 
WHEN @PERIOD=10 THEN GLU.DB_UNITS_10+GLU.CR_UNITS_10 
WHEN @PERIOD=11 THEN GLU.DB_UNIT... 

任何想法?感謝您的幫助提前。

+0

怎麼樣DDL和樣本數據? sqlfiddle.com是一個很好的開始。 – 2014-09-05 21:22:31

+0

如果你打印@SQL',你會吐出什麼? – Taryn 2014-09-05 21:27:23

+0

嘗試將'@ SQL'聲明爲'VARCHAR'。 – 2014-09-05 21:28:20

回答

0

你需要轉換爲VARCHAR100以及這樣的:

... 
AND GLU.ACCT_UNIT='''+CAST(100 AS VARCHAR(3))+''' 
... 

,或只是將其添加爲一個像這樣的字符串:

... 
AND GLU.ACCT_UNIT='''+'100'+''' 
... 
0

感謝PM77-1和gotqn

我嘗試了你的建議,現在我得到了這個:必須聲明標量變量「@PERIOD」。

這是我修改後的代碼按你的建議:

DECLARE 
@FISCAL_YEAR AS VARCHAR(4), 
@PERIOD AS VARCHAR(2), 
@COL AS VARCHAR(MAX), 
@SQL AS VARCHAR(MAX) 

SELECT @COL= COALESCE (@COL+ ',','')+QUOTENAME(MX_VALUE)FROM 
(SELECT DISTINCT MX_VALUE FROM AUMXVALUE WHERE MATRIX_CAT='_DIV') AS A 

SET @FISCAL_YEAR = 2014 
SET @PERIOD=6 


    SET @SQL= 
N' 
SELECT CURR.COMPANY, CURR.ACCT_UNIT, CURR.DESCRIPTION, CURR.ACCOUNT, 
(
(SELECT UNITS.* , GLN.DESCRIPTION FROM 
(SELECT 
GLU.COMPANY, 
GLU.ACCT_UNIT, 
GLU.ACCOUNT, 
AMV.MATRIX_CAT, 
'[email protected]+', 
CASE 
WHEN @PERIOD=1 THEN GLU.DB_UNITS_01+GLU.CR_UNITS_01 
WHEN @PERIOD=2 THEN GLU.DB_UNITS_02+GLU.CR_UNITS_02 
WHEN @PERIOD=3 THEN GLU.DB_UNITS_03+GLU.CR_UNITS_03 
WHEN @PERIOD=4 THEN GLU.DB_UNITS_04+GLU.CR_UNITS_04 
WHEN @PERIOD=5 THEN GLU.DB_UNITS_05+GLU.CR_UNITS_05 
WHEN @PERIOD=6 THEN GLU.DB_UNITS_06+GLU.CR_UNITS_06 
WHEN @PERIOD=7 THEN GLU.DB_UNITS_07+GLU.CR_UNITS_07 
WHEN @PERIOD=8 THEN GLU.DB_UNITS_08+GLU.CR_UNITS_08 
WHEN @PERIOD=9 THEN GLU.DB_UNITS_09+GLU.CR_UNITS_09 
WHEN @PERIOD=10 THEN GLU.DB_UNITS_10+GLU.CR_UNITS_10 
WHEN @PERIOD=11 THEN GLU.DB_UNITS_11+GLU.CR_UNITS_11 
WHEN @PERIOD=12 THEN GLU.DB_UNITS_12+GLU.CR_UNITS_12 
ELSE 0 END AS ACT 
FROM GLUNITS GLU JOIN GLNAMES GLN 
ON GLU.COMPANY=GLN.COMPANY 
AND GLU.ACCT_UNIT='''+'100'+''' 
JOIN AUMXVALUE AMV 
ON GLN.OBJ_ID=AMV.OBJ_ID 
AND AMV.MATRIX_CAT='''+'_DIV'+''' 
AND [email protected]_YEAR 
AND GLU.ACCOUNT=9100 
)UNITS 
JOIN GLNAMES GLN 
ON UNITS.COMPANY=GLN.COMPANY 
AND UNITS.ACCT_UNIT=GLN.ACCT_UNIT) CURR 
) T PIVOT(SUM(ACT) FOR MX_VALUE IN (' + @COL + ')) AS PIVTBL' 


EXEC (@SQL) 

感謝