我很难调试这个SQL查询,我得到了这个错误:
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '('.
Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'FOR'.
这是我的疑问:
SELECT SID,CSID,M,QCL,STUFF((SELECT distinct ',' + QUOTENAME(TN) from tmpTbl FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
from
(
select t.SID, t.CSID, t.M, w.QCL,t.TC,t.TN
from tmpTbl AS t
inner join WoOr AS w
on t.WO = WoOr.WO
where t.IsSelected = 0
) x
pivot
(
count(TC)
for TN in (STUFF((SELECT distinct ',' + QUOTENAME(TN) from tmpTbl FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,''))
) p
感谢您提前!
答案 0 :(得分:1)
for TN in (STUFF((SELECT distinct ',' + QUOTENAME(TN) from tmpTbl FOR XML
PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,1,''))
使用pivot功能,您无法执行动态列。除非你使用动态sql,否则它们需要硬编码。