Sql server 将XML数据插入SQL Server表

Sql server 将XML数据插入SQL Server表,sql-server,sql-server-2005,tsql,Sql Server,Sql Server 2005,Tsql,我的数据如下所示: <products> <product ProductID="1" Price="79.99" Weight="30.00" Quantity="1"> <addon ProductAddonID="0" ControlTypeID="9" Price="25.00" Weight="0.00" Quantity="1" Name="yyy" Data="ASD" /> <addon P

我的数据如下所示:

<products>
    <product ProductID="1" Price="79.99" Weight="30.00" Quantity="1">
        <addon ProductAddonID="0" ControlTypeID="9" Price="25.00" Weight="0.00" Quantity="1" Name="yyy" Data="ASD" />    
        <addon ProductAddonID="89" ControlTypeID="0" Price="15.00" Weight="4.00" Quantity="1" Name="xxx" Data="" />
    </product>
</products>
当我有多个产品/插件时,所有的插件都插入了最新的@OrderItemID。。。我不知道如何在我的SQL中工作,该SQL将插件插入到通过产品节点进行迭代的循环中

谁能给我指一下正确的方向吗

提前谢谢

我想

您需要在循环中插入记录以获取作用域标识

首先将Order.Items数据放入临时表中,然后在其上循环以插入Order.Items表

以下是想法——不是工作代码。

DECLARE @count INT
DECLARE @id INT

SET @count = 1
SET @id = totalNumberOfRecordsInTempTable -- Get records from xml to temp table first


WHILE @count <= @id
BEGIN
    INSERT INTO YourTable (Column1, Column2, ...)
    SELECT Column1, Column2, ... FROM SourceTable WHERE Id = @count

    SET @count = @count + 1

    SET @OrderItemId = SCOPE_IDENTITY()

    INSERT INTO Order.AddOns

END

不是真的想使用临时表。
DECLARE @count INT
DECLARE @id INT

SET @count = 1
SET @id = totalNumberOfRecordsInTempTable -- Get records from xml to temp table first


WHILE @count <= @id
BEGIN
    INSERT INTO YourTable (Column1, Column2, ...)
    SELECT Column1, Column2, ... FROM SourceTable WHERE Id = @count

    SET @count = @count + 1

    SET @OrderItemId = SCOPE_IDENTITY()

    INSERT INTO Order.AddOns

END
declare @table table
(
    id int,
    quanity int
)

insert into @table select 1, 10
insert into @table select 2, 20
insert into @table select 3, 30
insert into @table select 4, 40

declare @table2 table
(
    orderid int identity(1, 1),
    quanity int
)

declare @id int
select @id = max(id) from @table

while @id > 0
begin
    insert into @table2 (quanity)
    select quanity from @table where id = @id

    set @id = @id - 1

    select SCOPE_IDENTITY()
end