使用XQuery query()方法

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

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

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

SET @sampleXML='

                                     

                                              This is the first line

                                     

                                     

                                              This is the second line

                                      

                                  '

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

显示结果为:This is the first lineThis is the second line

同样可以使用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

你可能感兴趣的:(SQL,SERVER,2000/2005)