使用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

相关文章:

  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-01-09
  • 2022-12-23
  • 2021-05-23
猜你喜欢
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2022-01-16
  • 2021-06-12
  • 2022-12-23
  • 2022-03-07
相关资源
相似解决方案