Peter Bahaa's Blog

Business and Technical Articles

Best way to send multiple values in one parameter to database

July 19
by Peter Bahaa 19. July 2010 22:20

Many time i stucked with business requirements which says we need to send multiple parameters to database, so the first thing came to my head is to send a comma separated parameter and send this parameter to a t-sql tabler function and query on the values that come out from this function which was tooooooooooooo slowwwwwwwww

while ago i had this conversation with one of my colleges and he proposed X-Path which i tried it, and it was more slow and very pad performance, and another one suggested using OPENXML (http://msdn.microsoft.com/en-us/library/aa276847(SQL.80).aspx)

OPENXML provides a rowset view over an XML document. Because OPENXML is a rowset provider, OPENXML can be used in Transact-SQL statements in which rowset providers such as a table, view, or the OPENROWSET function can appear.

 

DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
      <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
      <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
   <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
      <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT    *
FROM       OPENXML (@idoc, '/ROOT/Customer',1)
            WITH (CustomerID  varchar(10),
                  ContactName varchar(20))

Here is the result set:

CustomerID ContactName          
---------- -------------------- 
VINET      Paul Henriot
LILAS      Carlos Gonzlez

If the same SELECT statement is executed with flags set to 2, indicating element-centric mapping, the

values of CustomerID and ContactName for both of the customers in the XML document are returned as NULL, because the <Customers> elements do not have any subelements.

Here is the result set:

CustomerID ContactName
---------- -----------
NULL       NULL
NULL       NULL

Tags:

Tips & Tricks | XML | T-SQL

Comments

8/30/2011 7:17:56 PM #

I really like the idea. Almost everything today can be found online, every document has a soft version.

fix slow computer performance

8/30/2011 7:29:41 PM #

A colleague told me this article, and I must admit it was worth it to read it.

cleaning up and defragmenting your registry

9/2/2011 7:06:16 PM #

Excellent post. I was checking constantly this blog and I'm impressed! Extremely helpful information specifically the last part  I care for such information much. I was looking for this certain information for a very long time. Thank you and best of luck.

credit repair firm

9/15/2011 6:14:44 PM #

The ideas are fantastic and the key things you said in truth,many comments are right and I think you should be better,and good luck in your life.

credit repair dispute letter

9/15/2011 6:15:07 PM #

The ideas are fantastic and the key things you said in truth,many comments are right and I think you should be better,and good luck in your life.

credit repair dispute letter

9/15/2011 6:15:51 PM #

The ideas are fantastic and the key things you said in truth,many comments are right and I think you should be better,and good luck in your life.

credit repair dispute letter

9/18/2011 1:47:11 AM #

Nice read. I just passed this onto a buddy who was doing a little research on that. He actually bought me lunch since I found it for him!  Therefore let me rephrase: Thanx for lunch!

SEO NJ

9/19/2011 3:33:28 AM #

Integrating with legacy systems often includes receiving comma-separated lists of string values

charge offs

9/23/2011 3:09:23 AM #

Pingback from freerob.com

HTML | Free Software | Free Templates | Freerob.com

freerob.com

9/26/2011 1:41:37 AM #

I just love the way you work. Thanks for sharing this great and interesting stuff. Fabulous post! I really enjoyed that.

credit repair companies

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar