1

I am attempting to write a stored procedure to insert about 1 million rows into a table from a C# app. My app writes out the rows to a XML file and I want the app to then call a stored procedure with the XML file as a parameter. I've been looking around at other similar questions but I can't seem to get them to work and none of them seem to explain their answers. My XML only has 2 columns so I believe it should be fairly simple. I am using SQL Server 2005 and ASP.NET 2.0. My XML file looks like this:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Test>
    <o_cus>0</o_cus>
    <o_depot>100001</o_depot>
  </Test>
1
  • So you have multiple <Test> elements inside <DocumentElement> then? Commented Nov 10, 2013 at 22:15

1 Answer 1

1

If your XML contains multiple <Test> elements inside the <DocumentElement>, then you could use something like this:

CREATE PROCEDURE dbo.InsertData(@Input XML)
AS
BEGIN
    INSERT INTO dbo.YourTable (O_cus, O_depot)
        SELECT
            XCol.value('(o_cus)[1]', 'int'),
            XCol.value('(o_depot)[1]', 'int')
        FROM 
            @input.nodes('/DocumentElement/Test') AS XTbl(XCol)
END

Basically, you're using XPath to get a list of <Test> XML elements from your @Input XML, and then you're grabbing each fragment's o_cus and o_depot elements as int (adapt as needed) and you insert those into your table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.