DECLARE @x XML = N''; Windows Step By Step Bill Zack 49.99 Developing ADO .NET Andrew Brust 39.93 Windows Cluster Server Stephen Forte 59.99
SET @x.modify('insert as first into (/catalog/book)[1]');
SET @x.modify('insert as last into (/catalog/book)[1]');
SET @x.modify('insert before (/catalog/book/author)[1]');
SET @x.modify('insert after (/catalog/book/author)[1]');
四個關鍵字:as first、as last、before、after。
-- 變數法 DECLARE @new XML = N'one two '; SET @x.modify('insert sql:variable("@new") into (/catalog/book)[1]'); -- 直接法 SET @x.modify('insert (one ,two ) into (/catalog/book/author)[1]');
DECLARE @v NVARCHAR(10) = N'變數插入';
SET @x.modify('insert attribute var {sql:variable("@v")} into (/catalog/book)[1]');
SET @x.modify('insert attribute name {"直接插入"} into (/catalog/book/title)[1]');
SET @x.modify('insert (attribute Id {"多值1"}, attribute name {"多值2"}) into (/catalog/book/author)[1]');
SET @x.modify('insert text{"文字內容"} as first into (/catalog/book)[1]');
SET @x.modify('insert before (/catalog/book/title)[1]');
SET @x.modify('insert before (/catalog/book/title)[1]');
SET @x.modify('
insert
if ((/catalog/book/title)[2]) then text{"第2本存在"}
else text{"第2本不存在"}
into (/catalog/book/price)[1]');
SET @x.modify('delete /catalog/book/@category'); -- 刪除屬性
SET @x.modify('delete /catalog/book/title[1]'); -- 刪除元素
SET @x.modify('delete /catalog/book/author[1]/text()'); -- 刪除文字內容
SET @x.modify('delete /catalog/book[2]'); -- 刪除整個節點
SET @x.modify('replace value of (/catalog/book/@category)[1] with "新分類"');
SET @x.modify('replace value of (/catalog/book/author/text())[1] with "新作者"');
SET @x.modify('
replace value of (/catalog/book/@category)[2] with
if (count(/catalog/book) > 4) then "多於4本" else "不超過4本"');
SELECT @x.query('/catalog');
SQL Server 2005+ 全版本支援 XML DML;Azure SQL Database 亦支援。