SQL Server Table中XML列的操作代碼
更新時(shí)間:2011年10月25日 14:40:12 作者:
SQL Server Table中XML列的操作代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
--創(chuàng)建測試表
DECLARE @Users TABLE
(
ID INT IDENTITY(1,1),
UserInfo XML
)
---插入測試數(shù)據(jù)
DECLARE @xml XML
SET @xml='
<root>
<user>
<userid>1</userid>
<userName>test1</userName>
</user>
</root>'
INSERT INTO @Users(UserInfo)VALUES(@xml)
--插入單節(jié)點(diǎn),(類型:as first,as last,after(默認(rèn)),before)
UPDATE @Users SET UserInfo.modify('insert <address>shanghai</address>
into (/root/user)[1]')
--插入多節(jié)點(diǎn)以','分割
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]')
---根據(jù) if 條件語句進(jìn)行插入
---判斷屬性值
UPDATE @Users SET UserInfo.modify('insert if(/root/user[@ID=1]) then (<tel>888888</tel>)
else (<qq>66666</qq>)
into (/root/user)[1]')
----判斷節(jié)點(diǎn)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 節(jié)點(diǎn)數(shù)是否小于等于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]')
相關(guān)文章
SQL Server開放(或關(guān)閉)端口連接的實(shí)現(xiàn)步驟
為了保證系統(tǒng)的安全性,可以關(guān)閉/開啟端口,本文主要介紹了SQL Server開放(或關(guān)閉)端口連接的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
關(guān)于sql server批量插入和更新的兩種解決方案
對于sql 來說操作集合類型(一行一行)是比較麻煩的一件事,而一般業(yè)務(wù)邏輯復(fù)雜的系統(tǒng)或項(xiàng)目都會涉及到集合遍歷的問題,通常一些人就想到用游標(biāo),這里我列出了兩種方案,供大家參考2013-04-04
清空MSSQL日志 與set recovery simple
清空MSSQL日志 與set recovery simple...2007-10-10
淺談tempdb在SqlServer系統(tǒng)中的重要作用
tempdb是SQLServer的系統(tǒng)數(shù)據(jù)庫一直都是SQLServer的重要組成部分,用來存儲臨時(shí)對象。tempdb中的任何數(shù)據(jù)在系統(tǒng)重新啟動之后都不會持久存在。因?yàn)閷?shí)際上每次SQLServer啟動的時(shí)候都會重新創(chuàng)建tempdb。這個(gè)特性就說明tempdb不需要恢復(fù)。2014-08-08
case?when?then?else?end語句的用法(附demo)
本文主要介紹了case?when?then?else?end語句的用法,主要介紹了兩種格式,簡單case函數(shù)和case搜索函數(shù),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
淺析SQL Server授予了CREATE TABLE權(quán)限但是無法創(chuàng)建表
這篇文章主要介紹了SQL Server授予了CREATE TABLE權(quán)限但是無法創(chuàng)建表的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
SQL Server統(tǒng)計(jì)信息更新時(shí)采樣百分比對數(shù)據(jù)預(yù)估準(zhǔn)確性的影響詳解
這篇文章主要給大家介紹了關(guān)于SQL Server統(tǒng)計(jì)信息更新時(shí)采樣百分比對數(shù)據(jù)預(yù)估準(zhǔn)確性影響的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
sqlserver數(shù)據(jù)庫如何修改實(shí)例名稱
這篇文章主要介紹了sqlserver修改實(shí)例名稱的實(shí)例代碼,代碼簡單易懂,修改完重啟服務(wù)器就可以了,感興趣的朋友跟隨小編一起看看吧2024-06-06

