我的表格:
从上面的表我想查询选择语句这样的东西:
Version ControlTestID TestPCID
02175bd8-69b5-4171-a9b6-f0e46ce6eb05 703A,703B,704A,704B
cdff4816-9217-4dba-a608-4247964a14bc BM002,BM003,BM004
应该显示如上格式......
我正在使用以下查询:
DECLARE @TBL TABLE(TESTERID VARCHAR(1000),VER_CONTROL_TESTID VARCHAR(1000))
declare @ColumnNameList nvarchar(1000),@ColumnNameList1 nvarchar(1000)
SELECT @ColumnNameList = coalesce(@ColumnNameList + ',' + TEST_DESCRIPTION, TEST_DESCRIPTION),
@ColumnNameList1=VER_CONTROL_TESTID
FROM LU_TDE_VER_CONTROL_TESTERID TESTERID
Inner join LU_TDE_VER_CONTROL VERCTRL on VERCTRL.VER_CONTROL_ID = TESTERID.VER_CONTROL_TESTID
INSERT INTO @TBL
SELECT @ColumnNameList ,@ColumnNameList1
sELECT TESTTB.*,CTRL.TESTPCID,CTRL.COMPONENT,CTRL.GROUP_NAME,CTRL.VERSION_LIST FROM LU_TDE_VER_CONTROL CTRL
inner join @TBL TESTTB on ctrl.VER_CONTROL_ID = TESTTB.VER_CONTROL_TESTID
以上查询正在运行,但显示错误的结果,如下所示。:
TestPCID VER_CONTROL_TESTID
703A,703B,704A,704B,BM002,BM003,BM004 CDFF4816-9217-4DBA-A608-4247964A14BC
之后我尝试了另外一种方法:
SELECT
SS.VER_CONTROL_ID,
STUFF((SELECT '; ' + US.TEST_DESCRIPTION
FROM LU_TDE_VER_CONTROL_TESTERID US
WHERE US.VER_CONTROL_TESTID = SS.VER_CONTROL_ID
FOR XML PATH(''), 1, 1, '') [SECTORS/USERS]
FROM LU_TDE_VER_CONTROL SS
GROUP BY SS.VER_CONTROL_ID
它会抛出错误:'XML'附近的语法不正确。
答案 0 :(得分:2)
你在FOR XML PATH('')
SELECT
SS.VER_CONTROL_ID,
STUFF((SELECT '; ' + US.TEST_DESCRIPTION
FROM LU_TDE_VER_CONTROL_TESTERID US
WHERE US.VER_CONTROL_TESTID = SS.VER_CONTROL_ID
FOR XML PATH('')), 1, 1, '') [SECTORS/USERS]
FROM LU_TDE_VER_CONTROL SS
GROUP BY SS.VER_CONTROL_ID