时间: 2021-07-31 作者:daque
创造多个带有各别参数的保存进程(stored procedure)来实行同一个工作老是一个很大的承担。运用xml字符串向你的保存进程发送参数就不妨简化这个工作;这也让com组件的安排更大略。
实行这个手段的本领是将你的参数动作一个xml字符串来传播,并领会xml来取回你所须要的数据,而后连接实行你所须要集成的功效。你不只不妨经过xml来获得少许参数,你还不妨对xml所创造的dom文书档案运转查问,以此来封装多个保存进程。我会供给少许例子,报告你即使实行这个手段,并简本地刻画每个例子。
在本例里,为了革新一个customer表格里的全名字段,我会传播几个参数。为了赢得customerid(身份列)和新的全名字段,xml会被领会。我传播给进程的xml字串就像底下的如许:
<root><customer><customerid>3</customerid><name>acme inc.</name></customer></root>
要被创造的保存字段就像底下的如许:
create procedure update_customer (@xmldatavarchar(8000)) asdeclare @customeridintdeclare @customernamevarchar(50)declare @xmldata_idint
exec sp_xml_preparedocument @xmldata_id output, @xmldata, ''
select @customerid = customerid, @customername = [name] from openxml(@xmldata_id, '//customer', 2) with (customeridint, [name] varchar(50))
exec sp_xml_removedocument @xmldata_id
update customer set customer.[name] = isnull(@customername, customer.[name])where customer.tblid = @customerid
这个进程开始就证明咱们将要用到的变量会生存关系消息。在此之后,dom文书档案被翻开,一个“句柄(handle)”会被归来到sp_xml_preparedocument挪用的第一参数里。
这个挪用的第二个参数是用来新dom文书档案的xml源文献。这个“句柄”是在举行openxml挪用的功夫用来从dom里查问消息的。openxml挪用的第二个参数是父节点的一个xpath映照,那些父节点包括有要被实行的数据。
第三个参数(2)指明,以元素为重心的映照会被运用。with子句为被领会的数据供给了数据列集(rowset)方法,sp_xml_removedocument挪用会删掉dom文书档案的源文献。
在底下这个例子里,我会传播一系列用户id,用以简略多个数据列。底下即是xml字符串的实质:
<root><customer><customerid>1</customerid></customer><customer><customerid>2</customerid></customer><customer><customerid>3</customerid></customer></root>
相映的保存进程看上去就像底下如许:. . .
exec sp_xml_preparedocument @xml_id output, @xmldata, ''
delete from customer where customer.tblid in (select customerid from openxml(@xmldata_id, '//customer', 2) with (customeridint))
. . .
有了这个保存进程就不复须要创造一个繁杂的sql查问字符串,用以在ado里传播大概屡次挪用一个保存进程了。这也会取消屡次挪用对搜集流量所形成的感化。
正如你不妨看到的,微软的sql 2000让所有进程稍微大略了一点。要记取,这一本领的不及之处在乎:在sql 2000举行xml工作的功夫,将xml动作一个参数发送会被控制到8,000字符。和往常一律,不要忽略了经心筹备的长处。
考察msdn库不妨赢得更多对于openxml、sp_xml_preparedocument以及sp_xml_removedocument的消息。