Stored Procedure Without Result

This example shows how to use a stored procedure that executes an INSERT query inside itself and doesn't return any result.

The script for creating a stored procedure and a table for the SQL Server DBMS is as follows:

CREATE TABLE dbo.Department (
  DeptNo int NOT NULL,
  DName varchar(14),
  Loc varchar(13),
  PRIMARY KEY (DeptNo)
)
GO
CREATE PROCEDURE dbo.Department_Insert
        @PDeptNo INT,
        @PDName VARCHAR(14),
        @PLoc VARCHAR(13)
AS
        INSERT INTO dbo.Department(DeptNo, DName, Loc) VALUES (@PDeptNo, @PDName, @PLoc);
GO

CREATE TABLE dbo.Department (
  DeptNo int NOT NULL,
  DName varchar(14),
  Loc varchar(13),
  PRIMARY KEY (DeptNo)
)
GO
CREATE PROCEDURE dbo.Department_Insert
        @PDeptNo INT,
        @PDName VARCHAR(14),
        @PLoc VARCHAR(13)
AS
        INSERT INTO dbo.Department(DeptNo, DName, Loc) VALUES (@PDeptNo, @PDName, @PLoc);
GO

First, we create a model and add a new method to it corresponding to the procedure (either at the stage of creation on the Select database objects page of Create Model Wizard, or by dragging the corresponding stored procedure from the Database Explorer window to the diagram area or in the Model Explorer window of an already existing model).

As a result, we get a method in the model corresponding to the stored procedure:

images_SP-without-Result-General-EF

With the following parameters:

images_SP-without-Result-Params-EF

As a result of code generation for the model, the corresponding method of the model context will be generated having a signature close to the relevant stored procedure:

C#:

public void DepartmentInsert (global::System.Nullable<int> PDeptNo, string PDName, string PLoc)

Visual Basic:

Public Sub DepartmentInsert (ByVal PDeptNo As Global.System.Nullable(Of Integer), ByVal PDName As String, ByVal PLoc As String)

Now it is possible to use this stored procedure in the application with the help of method wrapper. This allows working with the stored procedure with all possible convenience, as wrapper methods are strongly typed, are found by IntelliSense and have signatures close to the corresponding stored procedures.

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.