执行以下查询:
SELECT * FROM MY_DATA
FOR XML PATH('NODE'), TYPE, ROOT('ROOT')
我有以下输出:
<ROOT>
<NODE>
<THE_ID>11</Facility_ID>
<Station_ID>HA</Station_ID>
<Case_No>731</Case_No>
</NODE>
</ROOT>
我想知道的是否有可能产生如下输出:
<ROOT>
<NODE>
<THE_ID int>11</Facility_ID>
<Station_ID varchar="30">HA</Station_ID>
<Case_No varchar="50>731</Case_No>
</NODE>
</ROOT>
将类型作为属性。 有可能吗?
提前谢谢。
答案 0 :(得分:0)
以下是您所追求的信息。将其绑定到您的查询中并使用。
要将项目作为属性,请使用“as [@AttributeName]”。
例如,在您的示例中,将Select Station_Id部分更改为选择a,b,将Station_Id更改为[@Station_Id]。
DECLARE @Table VARCHAR(MAX) = 'YourTableName'
SELECT t.name AS [Table]
, c.name AS [Column]
, TYPE_NAME(c.user_type_id) AS [Type]
, c.max_length AS [Size]
, CASE WHEN c.is_nullable = 1 THEN ''
ELSE 'No Nulls'
END AS Nullable
, ROW_NUMBER() OVER ( PARTITION BY t.name ORDER BY c.name ASC ) AS rnk
FROM sys.columns AS c
JOIN sys.tables AS t ON c.object_id = t.object_id
WHERE t.name LIKE @Table
-- AND c.name IN ( 'thiscol', 'thatcol' )
ORDER BY t.name
, c.name