Hints And Tips research knowledge archive
Tip# 511
Sponsored Links
 
Subject: SQL Server 2005 : Passing arrays as parameters to SP
Updated: Dec-7-2007
Rating: Not Rated
By: Rocky Moore - Member #: 1
Location: Klamath Falls, Oregon USA
Website: www.RockyMoore.com
Category: Computers > Programming > Databases
Rate This Tip (10=Great):  1  2  3  4  5  6  7  8  9  10
Ever have the need to pass an array of data to a stored procedure in SQL Server 2005?  There are different ways to handle this, but one of the easiest is passing an XML datatype parameter.

I have heard some say it is complicated, but with SQL Server 2005 it is a breeze.

Recently, I had the need to pass an array of ints to a SP where it would act about the data in the database keyed on the int IDs I passed in.  Since I could have anywhere from a couple IDs up to a large number, it was simple to choose XML.

Here is a sample and how to pass a simple int array in XML:

This is the stored procedure that will return a 'select' of the array passed to it in XML -

create procedure playXml(@XmlText XML)
as

SELECT tab.num.value('.','int') as IntValue
FROM @XmlText.nodes('IntValues/IntValue') tab(num) 


Here is a sample of how to call it in a query:

DECLARE @XMLText XML 

SET @XMLText =' 
<IntValues> 
 <IntValue>10</IntValue>
 <IntValue>1</IntValue>
 <IntValue>-29</IntValue>
 <IntValue>887273</IntValue>
 <IntValue>234</IntValue>
 <IntValue>1</IntValue>
 <IntValue>1314</IntValue>
 <IntValue>156</IntValue>
</IntValues>'

execute playXml @XmlText



It is quite a simple matter to pass the XML field.  Building the query string for a basic array like this is easy.  In which ever technology you use, it is no harder than executing the stored procedure.

[Submit DIGG for this tip]


-----------
Note: Use the tips posted on this site at your own risk. The tips are posted by the public and as such may or may not be valid.

Sponsored Links
Categories
hints and tips gold divider
Sponsor Sites To Visit
The Awakening
Spirit-Filled Christianity is
more than filling a pew !

Not producing fruit?
Healing, where to turn?
Provision, where to turn?
Lost, where to turn?

It is time for --TheAwakening !
The Saints In Light
Christian Thoughts on
Various Topics - Check it Out!

Click Here
Silverlight City

Keep up with the latest in
Microsoft's Silverlight
Technology!

Click Here
Our Local Christians
Find other Christians
In your local area
Who share your beliefs!

Click Here
United Christian Voters
Tired of seeing your
Rights Vanish?
If all Christians would
Pull together, we can
bring Change Unite Now!

Click Here
Reflected Thought
Inventions ~ Ideas ~ Humor
Politics ~ Opinions ~ Thoughts
Software Development
Business And Stuff....

Click Here
XML RSS News Feed For Recently Posted Hints And Tips  RSS Feed
You can now get updates to the Recent Tips section by using RSS. The address is:

HintsAndTips.com/Rss.aspx

* Posting Tips *
To post a Tip, Recommendation or Tips Wanted, simply browse to the category you feel is a best fit for your post (click on the Recommention or Tips Wanted tab if fitting) and then click on "New Post" option.
(C) Copyright 1998-2008 All Rights Reserved By R & J Technologies - www.RJSoft.com