通过xml插入sql server中的多个表
我想插入多个表,即通过xml插入sql server中的多个表,sql,xml,sql-server-2008,xpath,xquery,Sql,Xml,Sql Server 2008,Xpath,Xquery,我想插入多个表,即客户、账户、账户交易 编辑 实体-客户一对一 客户-帐户映射为一对一 Account-AccountTransactions被映射为一对多 Entity(EntityId,EntityType)EntityId主键自动递增 Customer(CustomerId、FName、LName)CustomerId=EntityId主键 Account(AccountId,AccountNo,CustomerId)AccountId主键,CustomerId FK AccountT
客户、账户、账户交易
编辑
一对一实体-客户
映射为一对一客户-帐户
被映射为一对多Account-AccountTransactions
Entity(EntityId,EntityType)
EntityId主键自动递增
Customer(CustomerId、FName、LName)
CustomerId=EntityId主键
Account(AccountId,AccountNo,CustomerId)
AccountId主键,CustomerId FK
AccountTransactions(交易ID、付款日期、当前余额、账户ID)
TransactionId主键、账户ID 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>
阿披实
eba5d378-b
2/2/2012
500
2/2/2012
500
亚什
A101202
2/2/2012
500
2/2/2012
500
我想在xml中为每个客户插入客户、账户、交易
表,插入客户后,其id应保存回xml,并在账户
表中用作外键
我能看到的唯一方法是使用嵌套游标或嵌套while循环。有更好的方法吗?假设您有合适的表,您肯定可以在没有任何混乱的光标的情况下进行迭代 尝试这样的方法-现在这将处理客户和帐户,但您肯定也可以将其扩展到交易
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
现在,在下一步中,您可以解析每个客户/帐户对的事务,并将其插入相应的表中。您也可以使用SQLXML Bulkload组件执行此操作: 如何使用XML批量加载组件将XML导入SQL Server
我知道如果我有任何业务主键,就可以完成这项工作。我的实际数据库设计在某种程度上复制了AdventureWorks2008R2
BusinessEntity(BusinessEntityId,BusinessEntityType)
表,其中首先生成BusinessEntityId,然后将其插入到Person表中以创建Person。此后,其余实体流动。你知道怎么解决这个问题吗?