存储过程:将 XML 作为参数传递并插入(键/值对) [英] Stored procedure: pass XML as an argument and INSERT (key/value pairs)

查看:22
本文介绍了存储过程:将 XML 作为参数传递并插入(键/值对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何构造 XML 并将其作为参数传递给 MS SQL 2005 服务器上的存储过程?以及如何将 XML INSERT 插入到表格中?

数据采用键/值对的形式:

<预><代码>[0:[键,值],1:[键,值],2:[键,值]]

解决方案

举个例子:

/* 创建存储过程 */创建过程 ParseXML (@InputXML xml)作为开始声明@MyTable 表(身份证号码,值整数)插入@MyTable(身份证,价值)选择 Row.id.value('@id','int'), Row.id.value('@value','int')从@InputXML.nodes('/Rows/Row') 作为 Row(id)选择 ID,值来自@MyTable结尾去/* 创建 XML 参数 */声明@XMLParam xmlset @XMLParam = '<行><Row id="1" value="100"/><Row id="2" value="200"/><Row id="3" value="300"/></行>'/* 使用 XML 参数调用存储过程 */exec ParseXML @InputXML = @XMLParam/* 清理 - 删除程序 */删除过程 ParseXML去

How would you construct and pass XML as an argument to a stored procedure on an MS SQL 2005 server? And how would you INSERT the XML into a table?

The data is in the form of key/value pairs:

[
    0: [key, value],
    1: [key, value],
    2: [key, value]
]

解决方案

Here's one example:

/* Create the stored procedure */
create procedure ParseXML (@InputXML xml)
as
begin
    declare @MyTable table (
        id int,
        value int
    )

    insert into @MyTable 
        (id, value)
        select Row.id.value('@id','int'), Row.id.value('@value','int') 
            from @InputXML.nodes('/Rows/Row') as Row(id)        

    select id, value
        from @MyTable
end
go

/* Create the XML Parameter */
declare @XMLParam xml
set @XMLParam = '<Rows>
                     <Row id="1" value="100" />
                     <Row id="2" value="200" />
                     <Row id="3" value="300" />
                 </Rows>'

/* Call the stored procedure with the XML Parameter */
exec ParseXML @InputXML = @XMLParam

/* Clean up - Drop the procedure */
drop procedure ParseXML
go

这篇关于存储过程:将 XML 作为参数传递并插入(键/值对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆