在 FOR XML PATH 中包含 Column_Id 作为属性
Include Column_Id as atttribute in FOR XML PATH
让我们看看这个 sql 查询
SELECT *
FROM Customer
WHERE Id = 60
FOR XML PATH ('Customer'), ELEMENTS XSINIL, TYPE
它returnsXML像这样
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Id>60</Id>
<BranchId>5</BranchId>
<DocKind>3</DocKind>
<Document>SomeDoc</Document>
....
如何将 Column_id
作为 Xml 属性添加到每个节点?
这是返回 table 的 Column_Id
的查询 Customer
:
SELECT COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),
COLUMN_NAME, 'ColumnID') AS COLUMN_ID, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer'
我想要这样的结果:
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Id ColumnId=1>60</Id>
<BranchId ColumnId=2>5</BranchId>
<DocKind ColumnId=3>3</DocKind>
<Document ColumnId=4>SomeDoc</Document>
如果你在 SQL 服务器中,我想你可以为每个字段创建一个相关的子查询,例如:
(select top(1) COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),
COLUMN_NAME, 'ColumnID') AS COLUMN_ID
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer' AND COLUMN_NAME = 'BranchId') as 'BranchID/@Column_ID'
这是对 TSQL FOR XML PATH Attribute On , Type 中给出的信息的快速应用。
有了一些样本数据,就可以更容易地看出它是否有效...
CREATE TABLE Customer (ID INT IDENTITY(1,1), BranchID INT)
GO
INSERT INTO Customer SELECT ID FROM (VALUES (1),(2)) x(id)
GO
SELECT (SELECT TOP(1) COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),COLUMN_NAME, 'ColumnID')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer'
AND COLUMN_NAME = 'ID') AS 'ID/@Column_ID'
,ID
,(SELECT TOP(1) COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'ColumnID')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer' AND COLUMN_NAME = 'BranchId') AS 'BranchID/@Column_ID'
,BranchID
FROM Customer
WHERE Id = 1 FOR XML PATH ('Customer'), ELEMENTS XSINIL, TYPE
这让我感动:
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID Column_ID="1">1</ID>
<BranchID Column_ID="2">1</BranchID>
</Customer>
解决方法如下:
DECLARE @s VARCHAR(MAX) =
'select ' + STUFF(( SELECT ',' + CAST(COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA +
'.' + TABLE_NAME), COLUMN_NAME, 'ColumnID') AS VARCHAR(10)) +
' as ''' + COLUMN_NAME + '/@ColumnID'', [' + COLUMN_NAME + ']'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer'
FOR XML PATH('')), 1, 1, '') +
' from Customer FOR XML PATH(''Customer''),ELEMENTS XSINIL, TYPE'
EXEC (@s)
让我们看看这个 sql 查询
SELECT *
FROM Customer
WHERE Id = 60
FOR XML PATH ('Customer'), ELEMENTS XSINIL, TYPE
它returnsXML像这样
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Id>60</Id>
<BranchId>5</BranchId>
<DocKind>3</DocKind>
<Document>SomeDoc</Document>
....
如何将 Column_id
作为 Xml 属性添加到每个节点?
这是返回 table 的 Column_Id
的查询 Customer
:
SELECT COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),
COLUMN_NAME, 'ColumnID') AS COLUMN_ID, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer'
我想要这样的结果:
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Id ColumnId=1>60</Id>
<BranchId ColumnId=2>5</BranchId>
<DocKind ColumnId=3>3</DocKind>
<Document ColumnId=4>SomeDoc</Document>
如果你在 SQL 服务器中,我想你可以为每个字段创建一个相关的子查询,例如:
(select top(1) COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),
COLUMN_NAME, 'ColumnID') AS COLUMN_ID
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer' AND COLUMN_NAME = 'BranchId') as 'BranchID/@Column_ID'
这是对 TSQL FOR XML PATH Attribute On , Type 中给出的信息的快速应用。
有了一些样本数据,就可以更容易地看出它是否有效...
CREATE TABLE Customer (ID INT IDENTITY(1,1), BranchID INT)
GO
INSERT INTO Customer SELECT ID FROM (VALUES (1),(2)) x(id)
GO
SELECT (SELECT TOP(1) COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),COLUMN_NAME, 'ColumnID')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer'
AND COLUMN_NAME = 'ID') AS 'ID/@Column_ID'
,ID
,(SELECT TOP(1) COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'ColumnID')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer' AND COLUMN_NAME = 'BranchId') AS 'BranchID/@Column_ID'
,BranchID
FROM Customer
WHERE Id = 1 FOR XML PATH ('Customer'), ELEMENTS XSINIL, TYPE
这让我感动:
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID Column_ID="1">1</ID>
<BranchID Column_ID="2">1</BranchID>
</Customer>
解决方法如下:
DECLARE @s VARCHAR(MAX) =
'select ' + STUFF(( SELECT ',' + CAST(COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA +
'.' + TABLE_NAME), COLUMN_NAME, 'ColumnID') AS VARCHAR(10)) +
' as ''' + COLUMN_NAME + '/@ColumnID'', [' + COLUMN_NAME + ']'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customer'
FOR XML PATH('')), 1, 1, '') +
' from Customer FOR XML PATH(''Customer''),ELEMENTS XSINIL, TYPE'
EXEC (@s)