这将为您提供所需的 XML。
select T.name as TableName,
(
select ', '+C.name
from sys.columns as C
where C.object_id = T.object_id
order by C.column_id
for xml path(''), type
).value('substring((./text())[1], 3)', 'varchar(max)') as Columns
from sys.tables as T
order by T.name
for xml path(''), root('TableList')
但我认为这会返回一个更易于处理的 XML。
select T.name as TableName,
(
select ', '+C.name
from sys.columns as C
where C.object_id = T.object_id
order by C.column_id
for xml path(''), type
).value('substring((./text())[1], 3)', 'varchar(max)') as Columns
from sys.tables as T
order by T.name
for xml path('Table'), root('TableList')
或者也许像这样。
select T.name as TableName,
(
select C.name as ColumnName
from sys.columns as C
where C.object_id = T.object_id
order by C.column_id
for xml path(''), type
) as Columns
from sys.tables as T
order by T.name
for xml path('Table'), root('TableList')