欢迎光临
我们一直在努力

深入探讨SQL Server 2000对XML的支持(二)

建站超值云服务器,限时71元/月

使用 http 执行 sql 语句

  使用我们刚才创建的虚拟目录,我们就可以通过把sql查询语句写入url的方式执行查询。打开浏览器,在地址栏中写

入以下url:http://localhost/northwind?sql=select+ *+from+customers+where+customerid=anton

+for+xml+auto&root=root,如果你使用的虚拟目录别名不是northwind或者你使用一个远程服务器,只需要把相应的值改

掉就可以了。

  浏览器中会出现:

<?xml version="1.0" encoding="utf-8" ?>

<root>

<customers customerid="anton" companyname="antonio moreno taquería" contactname="antonio moreno"

contacttitle="owner" address="mataderos 2312" city="méxico d.f." postalcode="05023" country="mexico"

phone="(5) 555-3932" />

</root>

  让我们来分析一下这个url,“http://localhost/northwind”后面跟了一个sql查询语句,用来执行查询数据库

northwind的任务。在本例中,我们使用的查询语句是“select+*+from+customers+where+customerid=anton”。请注

意,这条语句已经被url编码过了,其中的空格都被替换成加号“+”,这样它才能被浏览器正确的传送到数据库中去,关

于url编码格式请您参阅相关文档。

  在查询语句之后,有添加了两个新的关键字:for xml和auto。for xml关键字可以对现有的关系数据库执行 sql 查

询,以返回 xml 文档形式。auto模式则将查询结果作为嵌套 xml 元素返回,在 from 子句内,每个在 select 子句中至

少有一列被列出的表都表示为一个 xml 元素,select 子句中列出的列映射到适当的元素特性,当指定了 elements 选项

后,表列映射到子元素而非特性。默认情况下,auto 模式将表列映射到 xml 特性。

  在for xml auto后,还需要添加一个参数“root”,其参数值作为返回的xml文件的root元素名。比如说,你可以把上

面我给出的例子中的root的参数值设为northwind,你会发现,返回的xml文件中除了root元素名变为northwind了以外,其

它都没有变化。

  上面我们说的是直接使用http中执行简单的查询,除此之外你还可以执行更加复杂的查询,比如说连接不同的表进行

查询,请看下面的例子,在下例中,select 语句连接了 northwind 数据库的中的 customers 和 orders 表,并返回信

息。

http://localhost/northwind?sql=select

customer.customerid%2ccustomer.contact

name%2c%5border%5d.orderid+from+customers+

customer+inner+join+orders+%5border%

5d+on+customer.customerid%3d%5border%

5d.customerid+for+xml+auto&root=northwind

因为返回的xml文件太长,我就不把它列出来了。

  如果你不想在customers表和orders表中出现嵌套的话,sql server 2000还提供另一个关键字用来替代auto,这个关

键字就是raw。raw 模式将查询结果集中的每一行转换为带有类属标识符 row 的 xml 元素。为了让您能够深入了解raw,

我再给出一个例子:使用 raw 模式检索客户和订单信息

  下面的查询返回客户和订单信息。在 for xml 子句中指定 raw 模式。

select customers.customerid, orders.orderid, orders.orderdate

from customers, orders

where customers.customerid = orders.customerid

order by customers.customerid

for xml raw

  下面是部分结果:

<row customerid="alfki" orderid="10643" orderdate="1997-08-25t00:00:00"/>

<row customerid="anatr" orderid="10308" orderdate="1996-09-18t00:00:00"/>

<row customerid="anatr" orderid="10625" orderdate="1997-08-08t00:00:00"/>

<row customerid="arout" orderid="10355" orderdate="1996-11-15t00:00:00"/>

  可以使用外部联接指定上面的查询在结果集中返回所有客户,无论这些客户是否有订单。

   select c.customerid, o.orderid, o.orderdate

     from customers c left outer join orders o on c.customerid = o.customerid order by c.customerid 

for xml raw

  下面是部分结果:

<row customerid="bonap" orderid="11076" orderdate="1998-05-06t00:00:00"/>

<row customerid="fissa"/>

<row customerid="paris"/>

<row customerid="ricsu" orderid="11075" orderdate="1998-05-06t00:00:00"/>

  我们还可以使用 http 执行存储过程,比如下面这个名为getxml的存储过程:

  create procedure getxml

   (

    @customerid varchar(5)

   )

   as

    begin

     select customerid, companyname,contactname

      from customers

      where customerid like @customerid + %

      for xml auto

  end

  为了执行这个存储过程并传送相应的参数,我们可以使用下面这段url,http://localhost/northwind?

sql=exec+getxml+a&root=root。这样,我们就能够在更高一层次使用存储过程,并且可以根据最终用户想要得到的结果

动态的改变参数值(比如在本例中,我们用的是“a”)。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 深入探讨SQL Server 2000对XML的支持(二)
分享到: 更多 (0)

相关推荐

  • 暂无文章