2011-08-01 39 views
1

說我有以下數據我想在我的FOR XML EXPLICIT查詢中使用:FOR XML EXPLICIT和精密

ItemId: 14528097 
ProductId: 7575 
Revenue: 12.95 
PerItemPrice: 12.95 
Quantity: 1 

現在,當我寫我的查詢,我得到下面的輸出:

<ItemId>14528097</ItemId> 
    <ProductId>7575</ProductId> 
    <Revenue>12.9500</Revenue> 
    <PerItemPrice>1.295000000000000e+001</PerItemPrice> 
    <Quantity>1.000000000000000e+000</Quantity> 

有什麼辦法得到它,使它看起來像這樣:

<ItemId>14528097</ItemId> 
    <ProductId>7575</ProductId> 
    <Revenue>12.9500</Revenue> 
    <PerItemPrice>12.95</PerItemPrice> 
    <Quantity>1</Quantity> 

還是我的運氣?

+0

您使用的PerItemPrice和收入是什麼數據類型?如果您使用REAL或FLOAT,也許您可​​以嘗試DECIMAL ... –

+0

好點。我目前正在使用CTE預加載一些內容;我會嘗試一些明確的演員,並很快回復。 –

+0

這就像一個魅力工作;你想發佈一個快速答案,所以我可以接受它? –

回答

2

好像a1ex07打我的答案,但我懷疑你使用FLOATREAL(有意或無意),並且我建議始終使用DECIMAL來表示不明確需要FLOAT/REAL的屬性的數值。

+0

+1。我只是注意到,你真的打敗了我的評論。我正在刪除我的.... – a1ex07

0

此查詢工作正常,所以一定要確保你有你的類型的正確鑄造:

declare @t table (
    ItemId varchar(10), 
    ProductId varchar(5), 
    Revenue decimal(9,2), 
    PerItemPrice decimal(9,2), 
    Quantity int) 

insert into @t select '14528097','7575',12.95,12.95,1 

select 1 as 'Tag', NULL as 'Parent', 
    NULL as 'Items!1!', 
    NULL as 'Item!2!Item', 
    NULL as 'Item!2!Revenue' 
union 
select 2, 1, NULL, ItemId, Revenue 
from @t for xml explicit 
0

對比使用浮點列與小數列的結果。

浮動...

create table #testfloat (a float) 
insert into #testfloat values (1) 
select * from #testfloat for xml auto 
drop table #testfloat 

<a="1.000000000000000e+000"/> 

現在有小數類型...

create table #testdec (a decimal(5,2)) 
insert into #testdec values (1) 
select * from #testdec for xml auto 
drop table #testdec 

<a="1.00"/>