sp_xml_preparedocument的使用

  /*大部分资料来自SQL Server Books */
sp_xml_preparedocument
sp_xml_preparedocument 返回一个句柄,可用于访问 XML 文档的新创建的内部表示方式。该句柄在连接到 Microsoft® SQL Server™ 2000 期间保持有效,直到重置连接或执行 sp_xml_removedocument 使句柄无效为止。
说明 
分析过的文档存储在 SQL Server 2000 的内部高速缓存中。MSXML 语法分析器使用 SQL Server 可用总内存的八分之一。若要避免内存不足,请运行 sp_xml_removedocument 以释放内存。
 
语法
sp_xml_preparedocument hdoc OUTPUT
[, xmltext]
[, xpath_namespaces]
 
参数
hdoc
是新创建的文档的句柄。hdoc 的数据类型为 integer。
 
[xmltext]
是原 XML 文档。MSXML 语法分析器分析该 XML 文档。xmltext 是 text 类型(char、nchar、varchar、nvarchar、text 或 ntext)的参数。默认值是 NULL,在这种情况下,将创建空 XML 文档的内部表示法。
 
[xpath_namespaces]
指定 OPENXML 的行和列 XPath 表达式中所使用的命名空间声明。默认值是 <root xmlns:mp='urn:schemas-microsoft-com:xml-metaprop'>。
xpath_namespaces 通过符合语法规则的 XML 文档的方式,为在 OPENXML 的 Xpath 表达式中使用的前缀提供命名空间 URI。xpath_namespaces 声明前缀必须用于引用命名空间 urn:schemas-microsoft-com:xml-metaprop,该命名空间提供有关分析后的 XML 元素的元数据。尽管可以使用此方法为元属性命名空间重新定义命名空间前缀,但此命名空间不会丢失。此前缀 mp 对 urn:schemas-microsoft-com:xml-metaprop 仍有效,即使 xpath_namespaces 不包含此类声明。xpath_namespaces 是 text 类型(char、nchar、varchar、nvarchar、text 或 ntext)的参数。
 
返回代码值
0(成功)或 >0(失败)
 
权限
执行权限默认授予 public 角色。
 
示例
A. 为符合语法规则的 XML 文档准备内部表示方式
下例返回作为输入提供的新创建的 XML 文档内部表示法的句柄。在对 sp_xml_preparedocument 的调用中,使用了默认命名空间前缀映射。
 
DECLARE @hdoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID='VINET' ContactName='Paul Henriot'>
   <Order CustomerID='VINET' EmployeeID='5' OrderDate='1996-07-04T00:00:00'>
      <OrderDetail OrderID='10248' ProductID='11' Quantity='12'/>
      <OrderDetail OrderID='10248' ProductID='42' Quantity='10'/>
   </Order>
</Customer>
<Customer CustomerID='LILAS' ContactName='Carlos Gonzlez'>
   <Order CustomerID='LILAS' EmployeeID='3' OrderDate='1996-08-16T00:00:00'>
      <OrderDetail OrderID='10283' ProductID='72' Quantity='3'/>
   </Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc
-- Remove the internal representation.
exec sp_xml_removedocument @hdoc
 
B. 为带 DTD 的符合语法规则的 XML 文档准备内部表示方式
下例返回作为输入提供的新创建的 XML 文档内部表示法的句柄。存储过程根据文档中包含的 DTD 来验证装载的文档。在对 sp_xml_preparedocument 的调用中,使用了默认命名空间前缀映射。
 
DECLARE @hdoc int
DECLARE @doc varchar(2000)
SET @doc = '
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE root
[<!ELEMENT root (Customers)*>
<!ELEMENT Customers EMPTY>
<!ATTLIST Customers CustomerID CDATA #IMPLIED ContactName CDATA #IMPLIED>]>
<root>
<Customers CustomerID='ALFKI' ContactName='Maria Anders'/>
</root>'
 
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc
 
C. 指定命名空间 URI
下例返回作为输入提供的新创建的 XML 文档内部表示法的句柄。在对 sp_xml_preparedocument 的调用中,保留了元属性命名空间映射的 mp 前缀,并将 xyz 映射前缀添加到了命名空间 urn:MyNamespace。
 
DECLARE @hdoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID='VINET' ContactName='Paul Henriot'>
   <Order CustomerID='VINET' EmployeeID='5'
           OrderDate='1996-07-04T00:00:00'>
      <OrderDetail OrderID='10248' ProductID='11' Quantity='12'/>
      <OrderDetail OrderID='10248' ProductID='42' Quantity='10'/>
   </Order>
</Customer>
<Customer CustomerID='LILAS' ContactName='Carlos Gonzlez'>
   <Order CustomerID='LILAS' EmployeeID='3'
           OrderDate='1996-08-16T00:00:00'>
      <OrderDetail OrderID='10283' ProductID='72' Quantity='3'/>
   </Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc, '<root xmlns:xyz='run:MyNamespace'/>'
 
注: sp_xml_preparedocument 在操作含有中文字符的 XML 语句中的注意事项
1.可以直接如下写法
DECLARE @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT, N'<root><qy><city>南海</city></qy></root>'
 
DECLARE @idoc int
DECLARE @doc nvarchar(1000)
set @doc=N'<root><qy><city>南海</city></qy></root>'
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
 
DECLARE @doc nvarchar(1000)必须字定义nvarchar类型,要是定义varchar就报错,不能存储创建
如下语句有问题,不正确。
DECLARE @idoc int
DECLARE @doc varchar(1000)
set @doc=N'<root><qy><city>南海</city></qy></root>'
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值