1

We have following XML data structure from which we would like to insert new data and update existing data in a SQL Server table.

Tables are SALES_HEADER and SALES_DETAILS.

SALES_HEADER columns

ID (PK)
INV_NO
DATE
CUST_ID (FK FROM CUSTOMER MASTER)

SALES_DETAILS columns:

ID (PK)
HEADER_ID (FK FROM SALES_HEADER)
STK_ID (FK FROM STK MASTER)
QTY
RATE
AMT

My XML is as under

<SALESDATA>
<SALESHEADER>
    <BILLNO>1</BILLNO>
    <DATE>01/08/2015</DATE>
    <CUSTOMERCODE>54</CUSTOMERCODE>
        <SALESDETAILS>
            <STK_ID>5</STK_ID>
            <QTY>10</QTY>
            <RATE>12</RATE>
            <AMOUNT>120<AMOUNT>
        </SALESDETAILS>
        <SALESDETAILS>
            <STK_ID>7</STK_ID>
            <QTY>9</QTY>
            <RATE>54</RATE>
            <AMOUNT>486<AMOUNT>
        </SALESDETAILS>
        <SALESDETAILS>
            <STK_ID>78</STK_ID>
            <QTY>62</QTY>
            <RATE>18</RATE>
            <AMOUNT>1116<AMOUNT>
        </SALESDETAILS>
</SALESHEADER>

5
  • Also, it would be helpful to know the data types of your columns, and whether or not the ID columns are IDENTITY or not ... Commented Aug 24, 2015 at 19:48
  • ID is int INV_NO = numeric(9, 0) DATE = date CUST_ID = int SALES_DETAILS columns: ID = int HEADER_ID = int STK_ID =int QTY = numeric(18, 0) RATE = numeric(18, 0) AMT = decimal(18, 0) Commented Aug 24, 2015 at 19:54
  • xml is xml file name H:\SDATA.XML Commented Aug 24, 2015 at 19:55
  • And is ID an IDENTITY column or not?? Commented Aug 24, 2015 at 20:27
  • yes id is and IDENTITY Column Commented Aug 24, 2015 at 20:30

1 Answer 1

1

You're not making it very easy for us to help you by obviously not mentioning a lot of crucial details about your task!

I tried to do something - at least to insert the header information into the table (or updating it, if it already exists).

Take this is a learning guide - and extend it to also cover the SALES_DETAILS table, which I REALLY do not know how to handle since you're not telling us some many things about your setup....... learn from my sample and do the second step yourself, as a learning experience!

-- Load XML from file
DECLARE @XmlContent XML 

SELECT @XmlContent = CAST(BulkColumn AS XML)
FROM OPENROWSET(BULK 'H:\SDATA.xml', SINGLE_BLOB) AS e;

SELECT @XmlContent

-- declare a temp table (or table variable) to hold the data
DECLARE @TempTable TABLE (BillNo NUMERIC(9, 0), SalesDate DATE, CustomerID INT, DetailsXML XML)

-- insert header data and the details XML into temporary table
INSERT INTO @TempTable (BillNo, SalesDate, CustomerID, DetailsXML)
    SELECT
        BillNo = XCHDR.value('(BILLNO)[1]', 'numeric(9,0)'),
        SalesDate = XCHDR.value('(DATE)[1]', 'date'),
        CustomerID = XCHDR.value('(CUSTOMERCODE)[1]', 'int'),
        DetailsXml = XCHDR.query('SALESDETAILS')
    FROM
        @XmlContent.nodes('/SALESDATA/SALESHEADER') XTHDR(XCHDR)

SELECT * FROM @TempTable

-- update those rows that already exist in the SALES_HEADER table
UPDATE 
    INV_NO = tt.BillNo,
    [Date] = tt.SalesDate,
    [CUST_ID] = tt.CustomerID
FROM dbo.SALES_HEADER hdr
INNER JOIN @TempTable tt ON tt.BillNo = hdr.INV_NO

-- insert those rows that don't exist yet - record the newly inserted ID values
INSERT INTO dbo.SALES_HEADER (INV_NO, [Date], CUST_ID)
    SELECT tt.BillNo, tt.SalesDate, tt.CustomerID 
    FROM @TempTable tt
    WHERE NOT EXISTS (SELECT * FROM dbo.SALES_HEADER hdr WHERE hdr.INV_NO = tt.BillNo)
Sign up to request clarification or add additional context in comments.

5 Comments

how to mention the file name in this?
AND IN SINGLE QUERY WANT TO INSERT IF NEW RECORD AND UPDATE IF EXISTING RECORD
I am very new to this, i dont know how to link path and file name to @xmlcontect and what if i want to update the existing data if invoice number is already exists
file may contain 5000 to 6000 Invoice records
error Must declare the scalar variable "@XmlContent".

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.