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

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

问题描述

你会如何构建,并通过XML作为参数传递给存储过程的MS SQL 2005服务器上?而且你会如何插入的XML到表?



的数据是键/值对的形式,

  [
0:[键,值],
1:[键,值]
2:[键,值]
]


解决方案

下面是一个例子:



<预类=郎-SQL prettyprint-覆盖> / *创建存储过程* /
创建过程ParseXML(@InputXML XML)

开始
声明@MyTable表(
ID INT,
值INT


将进入@MyTable
(ID,值)
选择Row.id.value('@ ID','诠释'),Row.id.value(@值, '诠释')从@ InputXML.nodes(
'/行/列')作为行(ID)

选择ID,从@MyTable
终值



/ *创建XML参数* /
申报@XMLParam XML
组@XMLParam ='<行和GT;
<行ID =1值=100/>
<行ID =2值=200/>
<行ID =3值=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天全站免登陆