我在 SQL Server 2012 中从 XML 文件进行了导入。一切正常,但出现以下问题:在 XML 文件中有两个项目(或更多或更少),并且 posid(posnummer)设置为“1”第一项。对于第二个项目,我搜索解决方案,将 PosId 增加为“1”,例如:“set @Posnummer = @Posnummer + 1”我发现没有可以插入此代码的地方。
这是代码:
DECLARE @bt varchar(30) = 'F';
DECLARE @Posnummer int = 1;
DECLARE @CurrentCustNo varchar(30) = '1006';
insert into BELEGP(Kennung, Belegtyp, Belegnummer, Posnummer, Postext, Menge, Eingabemenge, Editmenge, Artikelnummer, Bezeichnung, Preismenge, Einzelpreis, Gesamtpreis, Netto, Adressnr)
SELECT NEWID() as Kennung,
@bt as Belegtyp,
Myorder.j.value('OrderNumber[1]', 'varchar(50)') as Belegnummer,
@Posnummer as Posnummer,
@Posnummer as Postext,
MyItem.j.value('Quantity[1]', 'varchar(50)') as Menge,
MyItem.j.value('Quantity[1]', 'varchar(50)') as Eingabemenge,
MyItem.j.value('Quantity[1]', 'varchar(50)') as Editmenge,
MyItem.j.value('Id[1]', 'varchar(50)') as Artikelnummer,
MyItem.j.value('Name[1]', 'varchar(50)') as Bezeichnung,
MyItem.j.value('TotalPrice[1]', 'varchar(50)') as Preismenge,
MyItem.j.value('TotalPrice[1]', 'varchar(50)') as Einzelpreis,
MyItem.j.value('TotalPrice[1]', 'varchar(50)') as Gesamtpreis,
MyItem.j.value('UnityPrice[1]', 'varchar(50)') as Netto,
@CurrentCustNo as Adressnr
FROM (
SELECT CAST(x AS XML)
FROM
OPENROWSET( BULK 'D:\shop\xml\Bestellungen.xml',SINGLE_BLOB) AS k(x))
AS k(x)
CROSS APPLY x.nodes('Orders/Order') AS MYorder(j)
CROSS APPLY x.nodes('Orders/Order/Addresses/BillingAddress') AS MYAddresses(j)
CROSS APPLY x.nodes('Orders/Order/LineItems/LineItem') AS MYItem(j)
我该怎么做?