在SQL中访问XML字符串

    技术2022-05-11  112

    使用sp_xml_preparedocument可以将XML字符串缓存到SQLServer的内存中,并提供一个句柄来访问他

    使用OPENXML可以读取缓存中的数据

    使用sp_xml_removedocument可以释放掉缓存中的资源 

    下面是一个示例

    DECLARE   @XmlText   VARCHAR ( 8000 ) DECLARE   @ProductInfo   TABLE (    ItemNo  CHAR ( 10 ),    Description  varchar ( 200 )) DECLARE   @XmlHanlder   INT SET   @XmlText   =   ' <root>    <ProductInfo>        <ItemNo>00-000-001</ItemNo>        <Description>Keyboard</Description>    </ProductInfo>    <ProductInfo>        <ItemNo>00-000-002</ItemNo>        <Description>Hard Disk</Description>    </ProductInfo></root> '   EXEC  sp_xml_preparedocument  @XmlHanlder  OUTPUT,  @XmlText   INSERT   @ProductInfo (    ItemNo,    Description)  SELECT      ItemNo,Description  FROM OPENXML( @XmlHanlder ' /root/ProductInfo ' , 2 WITH  (    ItemNo  CHAR ( 10 ),    Description  varchar ( 200 ))  EXEC  sp_xml_removedocument  @XmlHanlder   SELECT   *   FROM   @ProductInfo

    最新回复(0)