简体   繁体   中英

Stored procedure output parameter returning rounded value

I'm using output parameters to return values from a stored procedure.

Declaration in stored procedure is : @GrandTtl DECIMAL(19,3) OUT

The SELECT query is:

SET @GrandTtl = (SELECT TOP 1 Bill_Amount 
                 FROM Tbl_Restaurant_Kitchen_Order_Bill_Details 
                 WHERE Bill_Number = @billno)

For example, the select query returns the value 4087.67 then the output parameter value is returned as 4088 from SQL Server to C#.

Here is the C# code calling the stored procedure:

SqlCommand cmd = new SqlCommand("Sp_RestCC_BillDetails", con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter OutParam26 = cmd.Parameters.Add("@GrandTtl", SqlDbType.Decimal,19);

da = new SqlDataAdapter(cmd);

con.Open();
da.Fill(ds, "dtRestCC_Items");
con.Close();

objRCCBEL.GrandTtlOut = Convert.ToDecimal(cmd.Parameters["@GrandTtl"].Value);

You need to set up the C# parameter as

  1. output -- obviously you've done this
  2. decimal type, with a correct/compatible scale

SqlParameter parm = new SqlParameter("@GrandTtl", SqlDbType.Decimal); 
parm.Precision = 19;
parm.Scale = 3;
parm.Direction = ParameterDirection.Output; 
cmd.Parameters.Add(parm);

If you do not set the scale, the default is 0. Ref: SqlParameter.Scale Property

The number of decimal places to which Value is resolved. The default is 0.

According to Microsoft decimal(p,s) should work for you. The money and smallmoneyt types are just a subset of decimal with precision of 4 places. So i think your problem comes from the type of the variable that is bound to the OUT parameter in C#.

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