简体   繁体   中英

How do I pass decimals to stored procedure without decimals rounding?

Hey I have a stored procedure that I pass parameters to. The parameter type is "structured" and uses a user defined table type. The table type looks something like this cut down version.

CREATE TYPE dbo.LineItem_TYPE AS TABLE
(

UnitTrade numeric (22,4), 
TotalBuy numeric (22,4),
TotalSell numeric (22,4), 
GrossProfit numeric (22,4), 
QuoteReference varchar (255)
)

The actual table matches this (the same precision ect.) but rounds the decimal values. What is wrong SQL gurus?

Check how you are using it against this sample.

CREATE TYPE dbo.LineItem_TYPE AS TABLE
(
    UnitTrade numeric (22,4), 
    TotalBuy numeric (22,4),
    TotalSell numeric (22,4), 
    GrossProfit numeric (22,4), 
    QuoteReference varchar (255)
);
GO
CREATE TABLE LineItem
(
    UnitTrade numeric (22,4), 
    TotalBuy numeric (22,4),
    TotalSell numeric (22,4), 
    GrossProfit numeric (22,4), 
    QuoteReference varchar (255)
);
GO
create proc dbo.uspInsertLineItem
@li dbo.LineItem_TYPE READONLY
AS
    set nocount on;
    insert LineItem
    select *
    FROM @li;
GO
declare @li dbo.LineItem_TYPE;
insert @li select
1.23451234,123456789.1111,10.50,1,'Quote1' union all select
999999999999999999.9999,1.1,2.2,null,'Quote1';
exec dbo.uspInsertLineItem @li;
GO
select * from LineItem

------- results

UnitTrade                TotalBuy        TotalSell  GrossProfit  QuoteReference
------------------------ --------------- ---------- ------------ ---------------
1.2345 ****              123456789.1111  10.5000    1.0000       Quote1
999999999999999999.9999  1.1000          2.2000     NULL         Quote1

You can see that all the numerics are faithfully stored to the correct number of decimal places, except the singular one marked * *. We put 1.23451234 into @li, which because it is a numeric(22,4) was already 1.2345 as soon as it entered the user-type. See this:

declare @li dbo.LineItem_TYPE;
insert @li (UnitTrade)
select 1.23451234;
select UnitTrade from @li

--------- result

UnitTrade
---------------------------------------
1.2345

You can try variable type float instead of numeric .

Also make sure that you pass the decimal / double variables to the stored procedure from your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM