Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database.SqlQuery calling stored procedure that has multiple output parameters

I have a stored procedure like below which takes 1 input parameter ( Name ) and returns 2 output parameters( EmployeeId and Salary). Our stored procedure will insert Name into Employee table and give us back EmployeeId and Salary.

CREATE PROCEDURE dbo.insertemployee
@iName varchar(500),
@OEmployeeId int OUTPUT,  
@OSalary Money OUTPUT

We are using EF Code First approach. I am able to insert records into employee table and cannot find how I can access my two output parameters. I know that I need to use like below. Can anybody tell me what must be Result. According to MSDN, it can be a class that has column names as properties. But my case is that we are not returning columns of my table but we are using two output parameters and I need know how to access those two output parameters @OEmployeeId and @OSalary.

context.Database.SqlQuery<Result>(" exec dbo.insertemployee....);

public class Result
{
   // what properties I must give here       
}
like image 649
Ziggler Avatar asked Feb 20 '14 20:02

Ziggler


People also ask

Can stored procedure have multiple output parameters?

A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.

Can we have multiple out parameters?

There can be more than one out parameter in a method. At the time of method call, out parameter can be declared inline.

How can we return two output parameter from stored procedure in SQL Server?

In order to fetch the multiple returned values from the Stored Procedure, you need to make use of a variable with data type and size same as the Output parameter and pass it as Output parameter using OUTPUT keyword. You can also make use of the Split function to split the comma separated (delimited) values into rows.

Which of the following is a valid syntax for executing a stored procedure with one out parameter of type integer?

CallableStatement is used in JDBC to call stored procedure from Java program.


1 Answers

The method you are trying to use only works for results of a query. It can't put the values of the output parameters into a new object for you automatically.

You have to create the parameters explicitly and read their values after the stored procedure has been run.

So if you have a stored procedure like this:

CREATE PROCEDURE dbo.insertemployee
(
    @iName varchar(500),
    @OEmployeeId int OUTPUT,  
    @OSalary Money OUTPUT
)
AS
BEGIN
    SELECT @OEmployeeId = 1337;
    SELECT @OSalary = 1000;
END

... you can execute it and get the results of the parameters like this:

using (var ctx = new Context())
{
    var nameParam = new SqlParameter("iName", "TestName");

    var employeeIdParam = new SqlParameter("OEmployeeId", SqlDbType.Int) 
    { 
        Direction = System.Data.ParameterDirection.Output 
    };

    var salaryParam = new SqlParameter("OSalary", SqlDbType.Money) 
    { 
        Direction = System.Data.ParameterDirection.Output 
    };

    ctx.Database.ExecuteSqlCommand(
        "insertemployee @iName, @OEmployeeId out, @OSalary out", 
        nameParam, employeeIdParam, salaryParam);

    var employeeId = (int)employeeIdParam.Value;
    var salary = (decimal)salaryParam.Value;
}
like image 180
Peter Hansen Avatar answered Oct 16 '22 02:10

Peter Hansen