Question

I have a really weird problem. I have a stored proc which has the following params:

@Id int OUTPUT,
@DataSourceId int, 
@OwnerId int, 
@Size numeric(16,6),
@SizeUnitId int,
@LayCanFrom datetime, 
@LayCanTo datetime, 
@DischargeDate datetime, 
@IMO int,
@ProductIds inttable readonly,
@LoadPortIds inttable readonly, 
@DischargePortIds inttable readonly, 
@LoadCountryIds inttable readonly, 
@DischargeCountryIds inttable readonly,
@LoadZoneIds inttable readonly, 
@DischargeZoneIds inttable readonly, 
@FreightRate numeric(16,6), 
@FreightRateUnitId int, 
@StateId int, 
@OriginalMessage varchar(max),
@Comments varchar(max) = null

The particular param of interest here is @Size numeric(16,6). On C# side, when I am calling the stored proc, I am declaring the param as:

var paramSize = command.Parameters.AddParameter("@Size", this.Size);
paramSize .SqlDbType = SqlDbType.Decimal;
paramSize .Size = 16;
paramSize .Precision = 6;

I am getting the connection string as:

using (var connection = new SqlConnection(
                (context.Connection as EntityConnection).StoreConnection.ConnectionString))
        {
            // Call function to execute the stored proc.
        }

Then I open the connection, and do the usual command.ExecuteNonQuery(); Now @Size is passed in as 75000.000000, which should be fine, as the field is numeric(16,6). In fact, I already have records in the same table, which have values like 75000.000000 in that Size column. However, when I call command.ExecuteNonQuery();, it comes back with

Parameter value '75000.000000' is out of range.

So, instead of passing in the parameter, I decided to default @Size in the stored proc to 75000.000000. And guess what? It works! Another interesting thins is that I am trying to capture the SP execution using SQL Profiler, and no matter what I try, or which events I select for capture, I am unable to see the SP execution. Is this because I am fetching my connection string from EF storeconnection? Has anybody encountered something like this before? This is driving me a bit crazy! :)

Was it helpful?

Solution

You should set Scale instead of Size:

paramSize.Precision = 16;
paramSize.Scale = 6;

Also note, that decimal (or numeric) is defined as:

 decimal [ (p[ ,s] )] and numeric[ (p[ ,s] )] 

The first number is a precision, and the second number is a scale. So, @Size numeric(16,6) means "precision is 16, and scale is 6".

You're confusing this too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top