SQL Server Table中XML列的作業碼

來源:互聯網
上載者:User

複製代碼 代碼如下:--建立測試表
DECLARE @Users TABLE
(
ID INT IDENTITY(1,1),
UserInfo XML
)
---插入測試資料
DECLARE @xml XML
SET @xml='
<root>
<user>
<userid>1</userid>
<userName>test1</userName>
</user>
</root>'
INSERT INTO @Users(UserInfo)VALUES(@xml)
--插入單節點,(類型:as first,as last,after(預設),before)
UPDATE @Users SET UserInfo.modify('insert <address>shanghai</address>
into (/root/user)[1]')
--插入多節點以','分割
UPDATE @Users SET UserInfo.modify('insert (<firstName>steven</firstName>,
<lastName>shi</lastName>) into (/root/user)[1]')
-- 增加屬性
declare @editTime varchar(23);
set @editTime=CONVERT(VARCHAR(23), GETDATE(), 121);
UPDATE @Users SET UserInfo.modify(
N'insert (attribute editTime {sql:variable("@editTime")})
into(/root/user/userid)[1]'
)
--插入多屬性以','分割
declare @aid float,@bid float
set @aid=0.5
UPDATE @Users SET UserInfo.modify('insert (attribute aid {sql:variable("@aid")},
attribute bid {"test"}
)
into (/root/user)[1]')
---插入注釋
UPDATE @Users SET UserInfo.modify(N'insert <!-- 注釋 -->
before (/root/user/userid[1])[1]')
---插入處理指示
UPDATE @Users SET UserInfo.modify('insert <?Program = "A.exe" ?>
before (/root)[1]')
---插入CDATA
UPDATE @Users SET UserInfo.modify(N'insert <C><![CDATA[<city>北京</city> or cdata]]> </C>
after (/root/user)[1]')
---插入文本
UPDATE @Users SET UserInfo.modify(N'insert text{"插入文本"} as first
into (/root/user)[1]')
---根據 if 條件陳述式進行插入
---判斷屬性值
UPDATE @Users SET UserInfo.modify('insert if(/root/user[@ID=1]) then (<tel>888888</tel>)
else (<qq>66666</qq>)
into (/root/user)[1]')
----判斷節點Value
UPDATE @Users SET UserInfo.modify('insert if(/root/user[firstName="steven1"]) then (<tel>1111</tel>)
else (<qq>2222</qq>)
into (/root/user)[1]')
----判斷user 節點數是否小於等於10
UPDATE @Users SET UserInfo.modify('insert if (count(/root/user)<=10) then element user { "This is a new user" }
else () as last
into (/root)[1]')
SELECT * FROM @Users
--有命名空間的操作
--DECLARE @xml XML
-- SET @xml='<root xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/users">
-- <user>
-- <userid>1</userid>
-- <userName>test1</userName>
-- </user>
-- </root>'
-- INSERT INTO Users(UserInfo)VALUES(@xml)
-- UPDATE Users SET UserInfo.modify('
-- declare namespace UI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/users";
-- insert <UI:user>
-- <UI:firstName>steven2</UI:firstName>
-- </UI:user> as first
-- into (/UI:root)[1]')
-- SELECT * FROM Users
-- UPDATE Users SET UserInfo.modify('
-- declare namespace UI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/users";
-- insert attribute ID { "55" }
-- into (/UI:root/UI:user)[1]')
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.