Stored Procedure returns incorrect scalar value of -1, instead of return value
Asked Answered
P

4

7

I am trying to return a scalar value from a stored procedure. I actually want to return the ID of a newly created record, but I have simplified my problem down to a stored procedure that takes an int and attempts to return that same int. This always returns -1. Thank you very much for your help.

Web API Controller call

var idtest = dbconn.my_return_int(123);

The stored procedure:

ALTER PROCEDURE [dbo].[my_return_int]  
    @ID int  
AS  
BEGIN  
    SET NOCOUNT ON;  
    DECLARE @return as int

    SET @return = -999

RETURN @return
END   

The Context generated stored procedure call

public virtual int my_return_int(Nullable<int> iD)  
{  
    var iDParameter = iD.HasValue ?
        new ObjectParameter("ID", iD) :
        new ObjectParameter("ID", typeof(int));

    return (IObjectContextAdapter)this).ObjectContext.ExecuteFunction("my_return_int", iDParameter);  
}  
Poppycock answered 16/6, 2014 at 23:8 Comment(3)
Not sure this is possible in any sensible way... #16406021Vimineous
Thank you, DavidG for the article. It got me started down the right path. So what I did to solve this was change my Stored Procedure to return an ObjectResult<int?> instead of an int. Then I did a SingleOrDefault() on the results of the Stored Procedure call, which yielded my int return value. Here's the completed code.Poppycock
Did you tried that? #14131161 #10340250 #14735977 #22441396Wellborn
D
7

When you execute ObjectContext.ExecuteFunction the result is:

from MSDN: discards any results returned from the function; and returns the number of rows affected by the execution

I.e. it doesn't return the output parameter, because it doesn't know there is one. Besides, as you have called SET NOCOUNT ON; in your stored procedure, it doesn't even return the number of affected rows, thus you get the -1.

So, you must do two changes:

  1. change your procedure so that it doesn't return the value, but selects it, i.e. instead of RETURN @return do SELECT @return AS alias. NOTE that you need the "AS alias" part. The alias can be whatever column name you want.
  2. change the mapping of the stored procedure, so that it expects an int32 value. If you have doubt, refer to this Q&A.

In this way you'll read the return value as a result set, instead of a return value.

Detail answered 15/1, 2015 at 11:59 Comment(1)
The included link to www.devtoolshed.com is forwarding to seemingly random sites.Arrhythmia
H
6

to solve this entity framework issue and assuming you already have a stored procedure working and you don't want to fix the places where it works good using other ways then EF you can replace the:

RETURN(0)

with:

BEGIN
    SELECT(0)
    RETURN(0)
END

that is if its a part of an IF sentence,
theמ go to: EF model (.edmx) -> Model Browser -> Function Imports -> (doubl click your func) ->Scalars -> Int32

now your return value is:

ObjectResult<Nullable<int>>

you can receive it by writing:

int? response = 0;
response = myEntity.myStoredProcedure(var1, var2).FirstOrDefault();

or in pics:
1.
Model Browser
2.
Function Imports
3.
Scalars

doing all this makes the EF model (.edmx) also supporting the 'Update Model from Database...' and you can update it without worry to changes you just made.

Hemicellulose answered 15/1, 2015 at 9:43 Comment(1)
All sorts of this. This is the only correct answer.Monachism
P
1

Thank you, DavidG for the article. It got me started down the right path. So what I did to solve this was change my Stored Procedure to return an ObjectResult<int?> instead of an int. Then I did a SingleOrDefault() on the results of the Stored Procedure call, which yielded my int return value. Like this:

Stored Proc:

-- RETURN @return does not work. Can't return a scalar value  
SELECT @return  -- This returns a result set with a single object that contains an int.

Then, my generated code looks like above but instead of returning an int it returns an ObjectResult<int?>

and I read the results like this:

var id = myDbContext.My_Return_Int(123).SingleOrDefault();
Poppycock answered 17/6, 2014 at 15:4 Comment(0)
C
0

I've just implemented another option in my project. I feed in another output parameter for @return.

Carolanncarole answered 17/9, 2014 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.