使用XQuery query()方法

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

posted @ 2007-08-23 16:23  ♂风车车  阅读(462)  评论(0编辑  收藏  举报