May 25, 2010 at 7:58 am
Hi.
What is the most efficient way of sending/passing parameters to a stored procedure from ADO.NET ?
Should I concatenate all the fields into a pipe delimited text stream and unpack in the procedure ?
Is there a way to pass xml into a procedure and unpack that ?
Thanks...
May 25, 2010 at 8:13 am
If you pass XML, then you'll need to shred it just like if you pass delimited data that needs to be parsed. A lot of folks use XML for such a thing but I generally prefer just delimited data. Delimited data is a little easier on the the network because it doesn't have all the tag overhead to pass.
With that in mind, please see the following article...
http://www.sqlservercentral.com/articles/T-SQL/63003/
Pardon the "error" in the article... somewhere along the line, I forgot to flag things as the introduction would lead you to believe but it doesn't affect the content of the article.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 25, 2010 at 1:35 pm
I tend to use a delimited list if I have a predefined, repeating structure when calling the sproc.
But if I get something like "3 variable columns out of 20" I prefer using XML since it's easier to extract the name and the corresponding value.
So, I'd say "it depends". 😉
May 25, 2010 at 3:55 pm
Heh... yep... depends on whether you want to be nice to the "pipe" or not.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 25, 2010 at 4:15 pm
Jeff Moden (5/25/2010)
Heh... yep... depends on whether you want to be nice to the "pipe" or not.
I have to admit that I try to balance "pipe load", overall performance and the effort I need to take to make it work. Sometimes the latter takes precedence as long as the middle one is acceptable not leaving enough room for the former 😀
May 26, 2010 at 1:55 am
But would I be right in saying that:
* For SS 2000 there is no easy way to unpack xml or perhaps OpenXml ?
* For SS 2005/2008 there is XQuery to unpack xml ??
May 26, 2010 at 3:03 am
Well, if I accept the overhead of passing xml, the advantage to me any way seems to be the procedure is easier to debug and read because the data items are tagged. If I just pass a pipe-delimited text stream then it does make testing and debugging more difficult, particularly where I may be passing 30+ parameters for a large insert/update.
I had a play with openXML with SS 2000 and on the face of it seems a reasonable solution:::
if exists(select name from sysobjects where name = 'TestOpenXML')
drop procedure dbo.TestOpenXML
go
create procedure dbo.TestOpenXML
(
@i_strXML varchar(2000)
)
as
declare @t_XMLDocPointer int
exec sp_xml_preparedocument @t_XMLDocPointer output, @i_strXML
declare @t_name varchar(30)
declare @t_town varchar(50)
declare @t_county varchar(50)
declare @t_postcode varchar(12)
declare @t_age int
select @t_name = name,
@t_town = town,
@t_county = county,
@t_postcode = postcode,
@t_age = age
from openxml(@t_XMLDocPointer,'/root',2)
with ( name varchar(30),
town varchar(50),
county varchar(50),
postcode varchar(12),
age int )
if @@error <> 0
begin
raiserror('Error occured while using OPENXML', 16, 1)
return
end
select @t_name, @t_town, @t_county, @t_postcode, @t_age
exec sp_xml_removedocument @t_XMLDocPointer[/color]
-- EXECUTE THE PROCEDURE..
declare @i_strXML VARCHAR(2000)
set @i_strXML=
'<root>
<name>Fred Smith</name>
<town>Swindon</town>
<county>Wiltshire</county>
<postcode>SN1 3PZ</postcode>
<age>34</age>
</root>'
exec dbo.TestOpenXML @i_strXML
Works fine...;-)
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply