Saturday, January 29, 2011

Pass xml to stored procedure

In C#:

    1 // create XML string

    2 var dataToPass = new StringBuilder("<RowsOfNumbers>");

    3 dataToPass.AppendFormat("<id>{0}</id>", 1);

    4 dataToPass.AppendFormat("<id>{0}</id>", 2);

    5 dataToPass.AppendFormat("<id>{0}</id>", 3);

    6 dataToPass.Append("</RowsOfNumbers>");

    7 

    8 // call SP

    9 myDB.ExecuteStoredProcedure("GetXmlData", new object[] { dataToPass.ToString() });

IN SQL:

CREATE PROCEDURE [dbo].[GetXmlData]    (@dataIdsXml XML)

AS

    DECLARE @dataIdsTable TABLE (ID int)

 

    INSERT INTO @dataIdsTable (ID) SELECT ParamValues.ID.value('.','VARCHAR(20)')

    FROM @dataIdsXml.nodes('/RowsOfNumbers/id') as ParamValues(ID)

 

    SELECT [ID] FROM @dataIdsTable WHERE [ID]=1

GO   

No comments: