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

后端 未结 4 689
日久生厌
日久生厌 2021-01-02 20:36

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 p

4条回答
  •  日久生厌
    2021-01-02 21:04

    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.

提交回复
热议问题