Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedure returns int instead of result set

I have a stored procedure that contains dynamic select. Something like this:

ALTER PROCEDURE [dbo].[usp_GetTestRecords]      --@p1 int = 0,      --@p2 int = 0     @groupId nvarchar(10) = 0 AS BEGIN     SET NOCOUNT ON;      DECLARE @query  NVARCHAR(max)      SET @query = 'SELECT * FROM CUSTOMERS WHERE Id = ' + @groupId     /* This actually contains a dynamic pivot select statement */      EXECUTE(@query); END 

In SSMS the stored procedure runs fine and shows result set.

In C# using Entity Framework it shows returning an int instead of IEnumerable?

private void LoadTestRecords() {     TestRecordsDBEntities dataContext = new TestRecordsDBEntities();     string id = ddlGroupId.SelectedValue;      List<TestRecord> list = dataContext.usp_GetTestRecords(id); //This part doesn't work returns int     GridView1.DataSource = list; } 

Generated function for usp_GetTestRecords

public virtual int usp_GetTestRecords(string groupId) {     var groupIdParameter = groupId != null ?         new ObjectParameter("groupId", groupId) :         new ObjectParameter("groupId", typeof(string));      return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_GetTestRecords", groupIdParameter); } 
like image 249
Rod Avatar asked Aug 15 '13 02:08

Rod


People also ask

Can a stored procedure return a result set?

In addition to returning output parameters, a stored procedure can return a result set (that is, a result table associated with a cursor opened in the stored procedure) to the application that issues the CALL statement. The application can then issue fetch requests to read the rows of the result set cursor.

Can procedure have return type?

Return Value in SQL Server Stored ProcedureIn default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.

Is it mandatory to return value in stored procedure?

The RETURN statement is used to unconditionally and immediately terminate an SQL procedure by returning the flow of control to the caller of the stored procedure. It is mandatory that when the RETURN statement is executed that it return an integer value. If the return value is not provided, the default is 0.

How can we return multiple result sets in stored procedure using Entity Framework?

In order to get multiple result sets working we need to drop to the ObjectContext API by using the IObjectContextAdapter interface. Once we have an ObjectContext then we can use the Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal.


2 Answers

I get this when I have a stored procedure that includes an "exec" call into a temporary table, such as:

insert into #codes (Code, ActionCodes, Description) exec TreatmentCodes_sps 0 

It appears that Entity Framework gets confused as to what should be returned by the procedure. The solution I've come across is to add this at the top of the sproc:

SET FMTONLY OFF 

After this, all is well.

like image 93
Yelnic Avatar answered Sep 21 '22 15:09

Yelnic


I got the same problem, and found solution here

  1. Move to your .edmx
  2. At Model Browser Window/Function Imports find your procedure then double click it
  3. Change the return type to you want
  4. Save .edmx and check the return type again.

Screenshot

It should be what you need now.

like image 23
Circle Hsiao Avatar answered Sep 21 '22 15:09

Circle Hsiao