2012-09-13 126 views
0

在SQL Server多個表我想要做插入到多個表,即Customer, Account, AccountTransactions插入通過XML

編輯

  • Entity - Customer一對一的
  • Customer - Account被映射爲一比一
  • Account - AccountTransactions被映射爲一對多

Entity(EntityId, EntityType) ENTITYID主鍵自動遞增

Customer(CustomerId, FName, LName)客戶ID = ENTITYID主鍵

Account(AccountId, AccountNo, CustomerId) ACCOUNTID PK,客戶ID FK

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

我的XML是:

<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> 

我要插入Customer, Account, Transaction表在XML和在插入到客戶每個客戶的ID應該被保存回XML並在Account表用作外鍵

我可以看到唯一的辦法就是使用嵌套遊標或嵌套的while循環。有沒有更好的方法存在?

回答

1

假設你有適當的表格 - 你可以肯定地做一個迭代的方法,沒有任何雜亂的光標!

嘗試這樣 - 這會處理現在的客戶和帳戶,但您也可以將其延伸到交易。

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 

現在在接下來的步驟,你可以爲每個客戶/帳戶對解析事務,並將這些到相應的表中。

+0

我知道這可能如果我有任何業務已經完成首要的關鍵。我的實際數據庫設計有點複製了AdventureWorks2008R2'BusinessEntity(BusinessEntityId,BusinessEntityType)'表,其中BusinessEntityId是在第一個位置生成的,然後插入到Person表中以創建一個Person。此後其餘的實體流動。任何想法如何解決這個問題? –