Insertion into multiple tables in sql server via xml

可紊 提交于 2019-12-25 05:23:18

问题


I want to do inserts into multiple tables, i.e. Customer, Account, AccountTransactions

Edit

  • Entity - Customer one to one
  • Customer - Account is mapped as one to one
  • Account - AccountTransactions are mapped as one to many

Entity(EntityId, EntityType) EntityId primary key Auto incremented

Customer(CustomerId, FName, LName) CustomerId = EntityId primary key

Account(AccountId, AccountNo, CustomerId) AccountId PK, CustomerId FK

AccountTransactions(TransactionId, PaymentDate, CurrentBalance, AccountId) TransactionId PK, AccountId FK

My XML is:

<CustomerList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
       <Customer>
              <CustomerId/>
              <CustomerName>Abhishek</CustomerName>
              <AccountId/>
              <AccountNumber>eba5d378-b</AccountNumber>
              <Transactions>
                     <Transaction>
                <TransactionId/>
                            <PaymentDate>2/2/2012</PaymentDate>
                            <Amount>500</Amount>
                     </Transaction>
                     <Transaction>
                <TransactionId/>
                            <PaymentDate>2/2/2012</PaymentDate>
                            <Amount>500</Amount>
                     </Transaction>
              </Transactions>
          </Customer>
       <Customer>
              <CustomerId/>
              <CustomerName>Yash</CustomerName>
              <AccountId/>
              <AccountNumber>A101202</AccountNumber>
              <Transactions>
                     <Transaction>
                <TransactionId/>
                            <PaymentDate>2/2/2012</PaymentDate>
                            <Amount>500</Amount>
                     </Transaction>
                     <Transaction>
                <TransactionId/>
                            <PaymentDate>2/2/2012</PaymentDate>
                            <Amount>500</Amount>
                     </Transaction>
              </Transactions>
       </Customer>
</CustomerList>

I want to insert into Customer, Account, Transaction table for each customer in xml and upon insertion into customer its id should be saved back into xml and also used in Account table as a foreign key

I can see only way is to use nested cursor or nested while loop. Do any better method exists?


回答1:


Assuming you have the proper tables in place - you can definitely do an iterative approach without any messy kludgy cursor!

Try something like this - this will handle customers and accounts for now, but you can definitely extend this to the transactions, too.

declare @input XML = '... your XML here .....';

CREATE TABLE #CustAcct (CustomerName VARCHAR(50), CustomerID INT, AcctNumber VARCHAR(50), AcctID INT);

-- first extract customer and account into from the XML, using a common table expression    
WITH CustomersAndAccounts AS
(
   SELECT
       CustomerName = CL.Cust.value('(CustomerName)[1]', 'varchar(50)'),
       AcctNumber = CL.Cust.value('(AccountNumber)[1]', 'varchar(50)')
   FROM 
       @input.nodes('/CustomerList/Customer') CL(Cust)
)
INSERT INTO #CustAcct(CustomerName, AcctNumber)
    SELECT CustomerName, AcctNUmber
    FROM CustomersAndAccounts

-- insert customers into 'Customer' table    
INSERT INTO Customer(CustomerName)
    SELECT CustomerName
    FROM #CustAcct

-- update the temporary working table with the appropriate ID's from the 'Customer' table    
UPDATE #CustAcct
SET CustomerID = c.CustomerID
FROM Customer c
WHERE #CustAcct.CustomerName = c.CustomerName

-- insert values into 'Account' table from the working table   
INSERT INTO Account(CustomerID, AccountNumber)
    SELECT CustomerID, AcctNumber
    FROM #CustAcct

-- update the working table from the values inserted
UPDATE #CustAcct
SET AcctID = a.AccountID
FROM Account a
WHERE #CustAcct.CustomerID = a.CustomerID AND #CustAcct.AcctNumber = a.AccountNumber

SELECT * FROM #CustAcct

Now in a next step, you could parse the transaction for each customer/account pair and insert those into the appropriate table.




回答2:


You can also do this with the SQLXML Bulkload component:

How to import XML into SQL Server with the XML Bulk Load component
http://support.microsoft.com/kb/316005



来源:https://stackoverflow.com/questions/12413372/insertion-into-multiple-tables-in-sql-server-via-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!