with UniqueBasic as(SELECT
tab.name AS TableName,-- 表名称
idx.name AS UniqueName,-- 唯一约束的名称
col.name AS UniqueFieldName -- 唯一约束的表字段FROM
sys.indexes idx
JOIN sys.index_columns idxCol
ON(idx.object_id = idxCol.object_id
AND idx.index_id = idxCol.index_id
AND idx.is_unique_constraint =1)JOIN sys.tables tab
ON(idx.object_id = tab.object_id)JOIN sys.columns col
ON(idx.object_id = col.object_id
AND idxCol.column_id = col.column_id))SELECT A.TableName,A.UniqueName,(SELECT UniqueFieldName+','FROM UniqueBasic t1 WHERE t1.UniqueName=A.UniqueName FOR XML PATH(''))AS UniqueFieldNames
FROM UniqueBasic A
WHERE A.UniqueName ='Qywx_App_UniqueAgentId'GROUPBY A.TableName,A.UniqueName