![](/img/trans.png)
[英]How to Insert xml top level attribute nodes using a column value from other table in SQL server
[英]SQL Server insert Attribute to top node of xml
我想將屬性插入XML數據類型的根節點。 如果我知道根節點的名稱是Quote,那么可以這樣插入:
DECLARE @myDoc xml;
DECLARE @id int;
SET @id = 12345;
SET @myDoc = '<Quote>
<Close>
123
</Close>
</Quote>' ;
SET @myDoc.modify('
insert attribute ID {sql:variable("@id") }
into (/Quote)[1] ');
SELECT @myDoc;
如果我不知道根節點名稱怎么辦? 無論根名稱如何,我都想向根節點插入屬性。 我正在使用SQL Server 2008r2。 謝謝。
使用(/*)[1]
定位根節點:
SET @myDoc = '<Quote>
<Close>
123
</Close>
</Quote>' ;
SET @myDoc.modify('
insert attribute ID {sql:variable("@id") }
into (/*)[1] ');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.