Home > Database > Mysql Tutorial > SQL Server Table中XML列的操作代码

SQL Server Table中XML列的操作代码

WBOY
Release: 2016-06-07 18:02:58
Original
933 people have browsed it

SQL Server Table中XML列的操作代码,需要的朋友可以参考下。

代码如下:
--创建测试表
DECLARE @Users TABLE
(
ID INT IDENTITY(1,1),
UserInfo XML
)
---插入测试数据
DECLARE @xml XML
SET @xml='


1
test1

'
INSERT INTO @Users(UserInfo)VALUES(@xml)
--插入单节点,(类型:as first,as last,after(默认),before)
UPDATE @Users SET UserInfo.modify('insert
shanghai

into (/root/user)[1]')
--插入多节点以','分割
UPDATE @Users SET UserInfo.modify('insert (steven,
shi) 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
before (/root)[1]')
---插入CDATA
UPDATE @Users SET UserInfo.modify(N'insert 北京 or cdata]]>
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 (888888)
else (66666)
into (/root/user)[1]')
----判断节点Value
UPDATE @Users SET UserInfo.modify('insert if(/root/user[firstName="steven1"]) then (1111)
else (2222)
into (/root/user)[1]')
----判断user 节点数是否小于等于10
UPDATE @Users SET UserInfo.modify('insert if (count(/root/user)else () as last
into (/root)[1]')
SELECT * FROM @Users
--有命名空间的操作
--DECLARE @xml XML
-- SET @xml='
--
-- 1
-- test1
--

--
'
-- 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
-- steven2
--
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]')
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template