How to work with stored procedures

Stored procedure (function) is a set of statements that resides on server side and can be executed. Using dbForge Studio, you can create, edit, execute and delete stored procedures.

Creating a Stored Procedure

  1. In Database Explorer, navigate to the Procedures node.
  2. Select New Procedure from the shortcut menu.
  3. Input name of the trigger and type a script that will represent the trigger.

  4. Save the document by clicking the Update Database button on the bottom of the window, or the Save button on the Standard toolbar. If there are any errors in the SQL syntax, you will be notified about it.

CREATE PROCEDURE Syntax

CREATE PROCEDURE dbo.uspGetManagerEmployees
    @BusinessEntityID [int]
AS
BEGIN
    SET NOCOUNT ON;
    -- Use recursive query to list out all Employees required for a particular Manager
 WITH [EMP_cte]([BusinessEntityID], [OrganizationNode], [FirstName], [LastName], [RecursionLevel]) -- CTE name and columns
 AS (
SELECT e.[BusinessEntityID], e.[OrganizationNode], p.[FirstName], p.[LastName], 0 -- Get the initial list of Employees
 for Manager n
 FROM [HumanResources].[Employee] e
                 INNER JOIN [Person].[Person] p 
                 ON p.[BusinessEntityID] = e.[BusinessEntityID]
        WHERE e.[BusinessEntityID] = @BusinessEntityID
        UNION ALL
  SELECT e.[BusinessEntityID], e.[OrganizationNode], p.[FirstName], p.[LastName], [RecursionLevel] + 1 -- Join recursive member to anchor
        FROM [HumanResources].[Employee] e 
            INNER JOIN [EMP_cte]
            ON e.[OrganizationNode].GetAncestor(1) = [EMP_cte].[OrganizationNode]
                 INNER JOIN [Person].[Person] p 
                 ON p.[BusinessEntityID] = e.[BusinessEntityID

Creating a Stored Procedure with the Help of Snippet

To simplify the process of a Stored Procedure creation, you may use the CreateProcedure snippet.

  1. Click the New SQL button on the Standard toolbar.
  2. Right-click anywhere in the SQL document, and then click Insert Snippet. The snippets list will appear.

  3. Double-click the CreateProcedure snppet.
  4. The following code will be inserted to the document.

    Create Preocedure Snippet

  5. Edit the code accordingly and save the trigger. If there are any errors in the SQL syntax, you will be notified about it.

Editing a Stored Procedure

To edit a stored procedure

  1. In Database Explorer, navigate to the Procedures node and expand it.
  2. Right-click a procedure and select Edit Procedure from shortcut menu.

    or

    double-click a procedure.

Executing a Stored Procedure

Right-click a stored procedure in Database Explorer and select Execute.

The stored procedure editor opens if it was not opened before. If the procedure accepts parameters, you will see the parameters dialog where you can assign initial values for procedure arguments.

Once the stored procedure is executed, you will see a corresponding record in the Output window. The data returned by the stored procedure will appear in Data view of the stored procedure editor.

Editing Parameters

Stopping a Stored Procedure

Click Stop Execution.

Deleting a Stored Procedure

Right-click a stored procedure in Database Explorer and select Delete.

Download dbForge Studio for SQL Server and try it absolutely free for 30 days!

Want to Find out More?

Overview

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the Studio in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the Studio for your business, request a demo to see it in action.
Ready to start using dbForge Studio for SQL Server?