SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SQL Store Procedure

Text copied!

« Previous
Next »

SQL Store Procedure

June 26, 2023, 5:46 p.m. under SQL

  • Hi! You should check CHOOSE function post first.

    In SQL, Store Procedure (SP) can be defined as a block/set of SQL statements that can be saved and executed whenever needed. It is used to perform a specific task or accomplish a particular purpose.

    It can be reused through multiple projects which reduces code redundancy. 'Code redundancy' refers to the practice of writing the same code multiple times within a project.

    Here's the syntax :
    CREATE PROCEDURE procedure_name
        @parameter1 data_type, -- optional
        @parameter2 data_type, -- optional
        ...
    AS
    BEGIN
    
        -- Write SQL statements here to perform desired operation
    
    END
            

    • Specify the store_procedure_name after the 'CREATE PROCEDURE' keyword.

    • Specify the parameter name using the "@" prefix and its data type. store procedure can be created without any parameter.

    • 'AS BEGIN' keyword indicates that the store procedure body has started.

    • 'END' keyword indicates that the store procedure body has ended.

    Input Parameter :

    In SQL Server, store procedure accepts many or no parameter. A parameter is a value that is passed into a store procedure to perform specific task.

    Calling store procedure :

    Calling store procedure simply means executing or running it. To call a store procedure in SQL Server, you can use the 'EXEC' statement in a SQL query.

    Here's the syntax :

    Without Parameter -

    EXEC store_procedure_name;
            

    With Parameter -

    EXEC store_procedure_name @Parameter = Value;
            

    • Specify the store procedure name after the 'EXEC' keyword.

    • Specify the parameter name and input value after store procedure name.

    Here's an example of Store Procedure without Parameter :

    [i] Let's assume you want to create a store procedure that retrieve only 'Male' employees data from [Employees] table.

    SQL Store Procedure

    [ii] Run below SQL statement :

    CREATE PROCEDURE GetAllMaleEmployees
    AS
    BEGIN
        SELECT * FROM Employees
        WHERE Gender = 'Male';
    END
            
    SQL Store Procedure

    [iii] Above statement will create a new store procedure named as 'GetAllMaleEmployees'.

    [iv] Next, let's execute this store procedure. Run below SQL statement :

    EXEC GetAllMaleEmployees;
            
    SQL Store Procedure

    [v] Above statement retrieves employee data from [Employees] table whose gender is specified as 'Male'.

    Here's an example of Store Procedure with a Parameter :

    [i] Let's assume you want to create a store procedure that accepts employee_id as input parameter from [Employees] table.

    That means while executing store procedure, you can manually provide any specific employee_id value for this input parameters and thus you can control its execution based on your requirements.

    [ii] Run below SQL statement :

    CREATE PROCEDURE GetAllEmployeesById
    @Employee_Id INT
    AS
    BEGIN
        SELECT * FROM Employees
        WHERE Employee_Id = @Employee_Id;
    END
            
    SQL Store Procedure

    [iii] Above statement will create a new store procedure named as 'GetAllEmployeesById' that accepts employee_Id as input parameter from [Employees] table.

    [iv] Next, let's execute this store procedure. Run below SQL statement :

    EXEC GetAllEmployeesById @Employee_Id = 1;
            

    [v] In above statement, we passed value '1' as parameter value. This will retrieve employee data from [Employees] table where employee_id is equal to '1'.

    SQL Store Procedure
    To enhance your understanding of 'SQL store procedure', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What is a stored procedure in SQL?

    A stored procedure in SQL is a prepared SQL code that you can save, so the code can be reused over and over again.

    Where are SQL stored procedures?

    SQL stored procedures are stored in the database server.

    What are the two types of stored procedures?

    The two types of stored procedures are:
    a. System-defined stored procedures
    b. User-defined stored procedures

    Why are stored procedures used?

    Stored procedures are used for performance optimization, security, and reusability of code in database operations.
    Thank You! You should check Store Procedure vs. Function post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy