Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get output parameter value

Tags:

I'm using EF in my MVC project. In my project I need to use stored procedures. My problem is to use output parameter with stored procedures. I have no idea how to do that

like image 767
Tiffany Mulligan Avatar asked Feb 20 '12 20:02

Tiffany Mulligan


People also ask

What is output parameter in SQL?

Output parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module.

How do you use output parameters?

The Output Parameters in Stored Procedures are used to return some value or values. 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.

How can get output parameter value from stored procedure in SQL Server?

The easy way is to right-click on the procedure in Sql Server Management Studio (SSMS), select 'Execute stored procedure..." and add values for the input parameters as prompted. SSMS will then generate the code to run the procedure in a new query window, and execute it for you.


1 Answers

When you create your entity model, you should make sure that you include stored procedures. Then, create Function Imports for them:

  1. Open your Entity Model in Visual Studio
  2. The Model Browser should open on the right side of the screen
  3. In the Model Browser, you need to select the stored procedure and click Add Function Import to create the function for the stored procedure.
  4. A dialog box will open to select the stored procedure and return type

That's it. Now you can use that in code.

using (MyEntities myContext = new MyEntities ())             {                    System.Data.Objects.ObjectParameter output = new System.Data.Objects.ObjectParameter("OutputParameterName", typeof(int));                    myContext.GetCustomerCount(output);                    Console.WriteLine(output.Value);             } 
like image 173
Dana Addler Avatar answered Nov 06 '22 11:11

Dana Addler