復(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é)點,(類型:as first,as last,after(默認),before)
UPDATE @Users SET UserInfo.modify('insert address>shanghai/address>
into (/root/user)[1]')
--插入多節(jié)點以','分割
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 條件語句進行插入
---判斷屬性值
UPDATE @Users SET UserInfo.modify('insert if(/root/user[@ID=1]) then (tel>888888/tel>)
else (qq>66666/qq>)
into (/root/user)[1]')
----判斷節(jié)點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é)點數(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]')
您可能感興趣的文章:- SQLSERVER查詢所有數(shù)據(jù)庫名,表名,和字段名的語句
- SQL Server SQL高級查詢語句小結(jié)
- SQLServer中用T—SQL命令查詢一個數(shù)據(jù)庫中有哪些表的sql語句
- SQL語句實現(xiàn)查詢SQL Server服務(wù)器名稱和IP地址
- 關(guān)于SQL Server查詢語句的使用
- 詳解SQL Server的簡單查詢語句
- SqlServer 基礎(chǔ)知識 數(shù)據(jù)檢索、查詢排序語句
- SQL Server中Table字典數(shù)據(jù)的查詢SQL示例代碼