0

我尝试将 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)
4

1 回答 1

2

即使您可以将值 0.19 转换为 int,它也会被舍入为 0。我认为您必须以十进制形式获取值,然后进行转换:

declare @Data xml = '<TaxRate>0.19</TaxRate>'
select cast(@Data.value('TaxRate[1]', 'decimal(29, 2)') * 100 as int)
于 2013-08-13T12:48:16.870 回答