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, for example, like this one:

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 a model and add a new method to it corresponding to the procedure (either at the stage of creation at 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 will get a method in the model corresponding to the stored procedure:

images_SPwithoutResultGeneral

With the following parameters:

images_SPwithoutResultParameters

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

 

C#:

public static void DepartmentInsert(NHibernate.ISession session, System.Nullable<int> PDeptNo, string PDName, string PLoc)
        {
            NHibernate.IQuery query = session.GetNamedQuery(@"DepartmentInsert");
            query.SetParameter(@"PDeptNo", PDeptNo);
            query.SetParameter(@"PDName", PDName);
            query.SetParameter(@"PLoc", PLoc);
            query.List();
        }

Visual Basic:

Public Shared Sub DepartmentInsert ( _
    session As NHibernate.ISession, PDeptNo As System.Nullable(Of Integer), PDName As String, PLoc As String)
            Dim query As NHibernate.IQuery = session.GetNamedQuery("DepartmentInsert")
            query.SetParameter("PDeptNo", PDeptNo)
            query.SetParameter("PDName", PDName)
            query.SetParameter("PLoc", PLoc)
            query.List()
        End Sub

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.