我尝试将 XML 值(例如 0.19)转换为 INT 并用 * 100 进行计算。
但我得到一个错误,如:在 INT 类型中转换 nvarchar-Value '0.19' 时出错。
这是代码:(但带有“CAST”的行是问题)
insert into BELEGP(Kennung, Belegtyp, Belegnummer, Posnummer, Postext, Zeilentyp, Menge, Steuerprozent, Eingabemenge, Editmenge, Artikelnummer, Bezeichnung, Preismenge, Einzelpreis, Gesamtpreis, Netto, Adressnr)
SELECT NEWID() as Kennung,
'F' as Belegtyp,
Myorder.j.value('OrderNumber[1]', 'varchar(50)') as Belegnummer,
ROW_NUMBER() OVER (ORDER BY Myorder.j.value('OrderNumber[1]', 'varchar(50)')) as Posnummer,
ROW_NUMBER() OVER (ORDER BY Myorder.j.value('OrderNumber[1]', 'varchar(50)')) as Postext,
'A' as Zeilentyp,
MyItem.j.value('Quantity[1]', 'varchar(50)') as Menge,
CAST(Cast(MyItem.j.value('TaxRate[1]', 'nvarchar(50)') as nvarchar) as INT) * 100 as Steuerprozent,
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,
Myorder.j.value('CustomerNumber[1]', 'varchar(50)') 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)