使用XQuery query()方法

在sql server management studio ,新建查询.然后

DECLARE @sampleXML XML   --申明一个XML数据类型 的变量

DECLARE @Exists bit --在使用exist()方法是用到

SET @sampleXML='<root>

                                      <L1>

                                              <L2>This is the first line</L2>

                                      </L1>

                                      <L1>

                                              <L2>This is the second line</L2>

                                       </L1>

                                  <root>'

使用query()方法获取L2节点值: SELECT @sampleXML . query('/root/L1/L2)')

显示结果为:<L2>This is the first line</L2><L2>This is the second line</L2>

同样可以使用data()方法返回无XML标签的数据,或者使用exist()方法验证一个特定的节点是否存在

如下:

 SELECT @sampleXML . query('data(/root/L1[L2="This is the second line"])')

查询结果为: This is the second line

SET @Exists =@ sampleXML . exist('/root/L1/L2[text()="This is the first line"]')

SELECT @Exists

查询结果为:1

以下代码演示了将TEXT类型转换成XML:

SELECT CAST(textdata AS XML) FROM dbo.SomeTable WHERE SomeColumnID=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值