通过xml插入sql server中的多个表

时间:2012-09-13 19:24:40

标签: sql xml sql-server-2008 xpath xquery

我想插入多个表格,即Customer, Account, AccountTransactions

修改

  • Entity - Customer一对一
  • Customer - Account被映射为一对一
  • Account - AccountTransactions被映射为一对多

Entity(EntityId, EntityType) EntityId主键自动递增

Customer(CustomerId, FName, LName) CustomerId = EntityId主键

Account(AccountId, AccountNo, CustomerId) AccountId PK,CustomerId 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>

我想在xml中为每个客户插入Customer, Account, Transaction表,插入到客户后,其id应保存回xml,并在Account表中用作外键

我只能看到使用嵌套游标或嵌套while循环的方法。有没有更好的方法?

2 个答案:

答案 0 :(得分:2)

假设你有适当的表 - 你绝对可以做一个迭代的方法,没有任何凌乱的kludgy光标!

尝试类似这样的事情 - 这将暂时处理客户和帐户,但您也可以将此扩展到交易中。

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

现在,在下一步中,您可以解析每个客户/帐户对的交易,并将其插入相应的表格中。

答案 1 :(得分:0)

您也可以使用SQLXML Bulkload组件执行此操作:

如何使用XML Bulk Load组件将XML导入SQL Server中 http://support.microsoft.com/kb/316005