M
Martin H
I am having problems with the SQL generated by LINQ to execute a stored
procedure with Decimal output parameters. This simple example illustrates the
problem
CREATEPROCEDURE ShowLinqBug(
@paramValue Decimal(12,4) OUTPUT)
AS
BEGIN
SET @paramValue = 9999999.9999
END
go
When this is called from using LINQ using this procedure
private void button1_Click(object sender, EventArgs e)
{
try
{
DataClasses1DataContext context = new DataClasses1DataContext();
decimal? input = 0;
context.ShowLinqBug(ref input);
MessageBox.Show(input.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This is the SQL that is generated
declare @p3 numeric(1,0)
set @p3=0
declare @p4 int
set @p4=NULL
exec sp_executesql N'EXEC @RETURN_VALUE = [dbo].[ShowLinqBug] @Input = @p0
OUTPUT',N'@p0 decimal(1,0) output,@RETURN_VALUE int output',@p0=@p3
output,@RETURN_VALUE=@p4 output
select @p3, @p4
Notice that even though the stored procedure parameter is Decimal(12,4),
LINQ has converted this to numeric(1,0) based on the actual value passed in.
Id the value of input is set to null then linq will declare it as
numeric(29,0) , Because te stored procedure attempts to update @paramValue =
9999999.9999 it fails and raises an "Error converting decimal to decimal"
error. In this simple example it is easy (albeit ugly) to initialise the
variable to 9999999.9999 so a numeric(12,4) parameter will be passed. However
this can't easily be done if the stored procedure is being used to update a
Linq class and is mapped to a property.
I have for this problem being reporting elsewhere but I can't find anything.
I hope that this means that I am overlooking something trivial. Has anyone
got any fix for this behaviour or any otherwise a simple workaround?
procedure with Decimal output parameters. This simple example illustrates the
problem
CREATEPROCEDURE ShowLinqBug(
@paramValue Decimal(12,4) OUTPUT)
AS
BEGIN
SET @paramValue = 9999999.9999
END
go
When this is called from using LINQ using this procedure
private void button1_Click(object sender, EventArgs e)
{
try
{
DataClasses1DataContext context = new DataClasses1DataContext();
decimal? input = 0;
context.ShowLinqBug(ref input);
MessageBox.Show(input.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This is the SQL that is generated
declare @p3 numeric(1,0)
set @p3=0
declare @p4 int
set @p4=NULL
exec sp_executesql N'EXEC @RETURN_VALUE = [dbo].[ShowLinqBug] @Input = @p0
OUTPUT',N'@p0 decimal(1,0) output,@RETURN_VALUE int output',@p0=@p3
output,@RETURN_VALUE=@p4 output
select @p3, @p4
Notice that even though the stored procedure parameter is Decimal(12,4),
LINQ has converted this to numeric(1,0) based on the actual value passed in.
Id the value of input is set to null then linq will declare it as
numeric(29,0) , Because te stored procedure attempts to update @paramValue =
9999999.9999 it fails and raises an "Error converting decimal to decimal"
error. In this simple example it is easy (albeit ugly) to initialise the
variable to 9999999.9999 so a numeric(12,4) parameter will be passed. However
this can't easily be done if the stored procedure is being used to update a
Linq class and is mapped to a property.
I have for this problem being reporting elsewhere but I can't find anything.
I hope that this means that I am overlooking something trivial. Has anyone
got any fix for this behaviour or any otherwise a simple workaround?