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