Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OutPut parameter in SP with EF 4.0

I updated my Model with my Stored procedure and in the model browser I can see it has a Function import as well.
My SP inserts a record if none exists and returns a 1 else returns 0, pretty simple I think.

SP

CREATE PROCEDURE [dbo].[User_UpdateMessage]
(
@UserId int = 0, 
@UserId2 int = 0,
@Success bit = 0 OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;

IF NOT EXISTS ( SELECT [UserIdFrom] FROM  [dbo].[MessageUpdate] WHERE [UserIdFrom] = @UserId AND [UserIdTo] = @UserId2 )
BEGIN
    INSERT INTO [dbo].[MessageUpdate] ([UserIdFrom], [UserIdTo])
    VALUES (@UserId, @UserId2)
    SELECT @Success = 1;
END
ELSE
    SELECT @Success = 0;
END

In my code I am calling the SP:

// Output Parameter
System.Data.Objects.ObjectParameter paramSuccess1 = 
new System.Data.Objects.ObjectParameter("Success", typeof(byte));
_Entity.User_UpdateMessage(id, userId, paramSuccess1);

It is failing while executing the SP with the following error:

The data reader returned by the store data provider does not have enough columns for the query requested

[UPDATE]
As I was writing this I solved the problem. In the Model designer, the return type should be none, I had it to return Byte.

An answer to this question could be further enhancements or changes.

like image 697
Picflight Avatar asked Jun 04 '10 17:06

Picflight


People also ask

How do you pass an output parameter to a procedure?

To call a stored procedure with output parameters, you follow these steps: First, declare variables to hold the values returned by the output parameters. Second, use these variables in the stored procedure call.

What is an output parameter?

Output parameters are the parameters that are fetched from the response of a service call. These are formatted according to the attributes you configure for the output before displaying on the device. The service parameters have a scope and data type attached to them.

What is the use of output parameter in SQL Server?

A parameter whose value is sent out of the stored procedure/function module and back to the calling PL/SQL block is known as an output parameter. A variable, not a constant, must be used as an OUT parameter.


2 Answers

I too had the same problem, changed the return type to NONE instead of int32. Thanks saved me hours of debugging

like image 161
stephen ebichondo Avatar answered Jan 01 '23 00:01

stephen ebichondo


you can use a 'select' statement in your stored procedure . it will solve the issue

Reference: http://codetunnel.com/blog/post/83/what-to-do-if-entity-framework-function-import-does-not-detect-any-columns-being-returned-by-your-stored-procedure

like image 44
user1542653 Avatar answered Dec 31 '22 22:12

user1542653