Friday, March 9, 2012

pass xml dataset to sql server

I'm not sure if this is the correct forum or not, if not can someone direct me to the correct one?

I have a dataset that is generate from C#, the data set is XML format (generated from .NET) and has the column names of the table and data that I need to insert into. Is it possible to pass the dataset as is and have a stored procedure parse out the data that I need?

example: the xml looks like this

<NewDataSet>

<Table>

<CustID>1</CustID>

<CustName>Larry</CustName>

</Table>

</NewDataSet>

Is possible to pass this dataset to SQL just like this and have a stored procedure parse it out?

Maybe something like this:

declare @.myXml xml
set @.myXml =
'<NewDataSet>
<Table>
<CustID>1</CustID>
<CustName>Larry</CustName>
</Table>
</NewDataSet>'

declare @.iDoc int

exec sp_xml_preparedocument @.iDoc output, @.myXml

select * from
openxml (@.iDoc, 'NewDataSet/Table', 1)
with ( CustID integer 'CustID/text()',
CustName varchar(10) 'CustName/text()'
)

/*
CustID CustName
-- -
1 Larry
*/

|||

Sure you can. There are new features in SQL Server 2005 regarding XML.

declare @.x xml

set @.x = N'

<NewDataSet>

<Table>

<CustID>1</CustID>

<CustName>Larry</CustName>

</Table>

</NewDataSet>

'

select

t.c.query('./CustID').value('.', 'int') as CustID,

t.c.query('./CustName').value('.', 'varchar(50)') as CustID

from

@.x.nodes('NewDataSet/Table') t(c)

go

AMB

No comments:

Post a Comment