2012-09-13 99 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。此后其余的实体流动。任何想法如何解决这个问题? –