现在的位置: 首页 > 综合 > 正文

SQL XML的exist操作

2013年05月23日 ⁄ 综合 ⁄ 共 944字 ⁄ 字号 评论关闭

 --创建测试数据库

CREATE DATABASE mytest;
GO

USE mytest;
GO

--创建测试表
CREATE TABLE Users
(
    ID 
INT IDENTITY(1,1),
    UserInfo XML

)

/*****************XML 的 Exist 操作*****************************/    
     
    
---插入测试数据
    DECLARE @xml XML
    
SET @xml='<root>
    <user id="1">
        <userid>001</userid>
        <userName>test1</userName>
        <userName>test2</userName>
    </user>
    <user id="2">
        <userid>002</userid>
        <userName>test1</userName>
        <userName>test2</userName>
    </user>
    </root>
'
    
INSERT INTO Users(UserInfo)VALUES(@xml)
 

--delete Users
--
查询出存在userid=001 的XML,'True' 也可以是1
select * from Users where UserInfo.exist('/root/user[userid="001"]')='True'

--查询出存在user id=1 的XML
select * from Users where UserInfo.exist('/root/user[@id=1]')='True' 

--查询存在userid 节点的XML
select * from Users where UserInfo.exist('/root/user[userid]')='True'

 --查询不存在userid 节点的XML
select * from Users where UserInfo.exist('/root/user[not(userid)]')='True'

 

 

 

抱歉!评论已关闭.