Wednesday, March 7, 2012

pass several records at once for insert ?

If I was to pass several xml elements into a stored procedure (as an xml parameter), how could I then insert all the items into a table ?

Are you wanting to do something similar to what is done in this post:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1501246&SiteID=1

Here is an example that uses NODES:

Code Snippet

declare @.myXml xml

set @.myXml = N'<marketData>
<date>19-Apr-2007 17:08:55</date>
<Rates>
<Rate code="USDCAD">
<values>
<value type="BID">1.1276</value>
<value type="ASK">1.1277</value>
<value type="MID">1.127649997783</value>
</values>
</Rate>
<Rate code="EURUSD">
<values>
<value type="BID">1.3607</value>
<value type="ASK">1.3608</value>
<value type="MID">1.36075</value>
</values>
</Rate>
</Rates>
</marketData>'

select r.value ('http://@.code', 'varchar(20)') as rateCode,
r.value ('./@.type', 'varchar(20)') as type,
r.value ('.', 'varchar(20)') as value
from @.myXml.nodes('/marketData/Rates/Rate/values/value') as x(r)

No comments:

Post a Comment