EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

What is Procedure in SQL?

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » SQL Tutorial » What is Procedure in SQL?

What-is-Procedure-in-SQL

Introduction to Procedure in SQL

Procedures or Stored Procedures in SQL are logical units of SQL queries that can be created once, fetched and executed multiple times whenever there is a need to repeat the same operation. It also allows to pass parameters, like the stored procedure with arguments in the OOPS concept. It is categorized under DML (Data Manipulation Language) of SQL programming, as it is applicable on operations involving data management and manipulation.

What is SQL?

Structured Query Language (SQL) is sometimes pronounced as the individual character “S-QL” or “see-Quel”. It is used to query, manipulated or update data in the relational database. It is used to insert or update or delete records in a database. One of the main functionality of SQL is maintaining a database.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of SQL query to fetch all the records is as follows:

select * from table_name

Stored procedures in SQL

What is a Stored Procedure? A Stored Procedure is a collection of Structured Query Language (SQL) statements with a name assigned to it. These stored procedures are stored in a relational database management system (RDBMS). So it is possible for multiple calling of the procedures reducing multiple executions and resulting in reduced execution time. Data can be modified using procedures and the major advantage is that it is not tied to a particular application.

Stored procedures can accept parameters as an input and can return multiple values as an output parameter. In SQL Server stored procedures perform operations in the database and can return a status value to an application or a batch calling procedure. User-defined procedures are created using the user-defined database, with the exception when we have a read-only database. For that, we need to develop it in Transact SQL (T-SQL). Tempdb is where all the temporary procedures are stored.

There are two types of stored procedures we have

  1. Local procedures
  2. Global procedures

Local procedures are the one which is only visible for the user using it in the connection, whereas Global procedures are visible to any of the users in the same connection.

Popular Course in this category
SQL Training Program (7 Courses, 8+ Projects)7 Online Courses | 8 Hands-on Projects | 73+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,626 ratings)
Course Price

View Course

Related Courses
JDBC Training (6 Courses, 7+ Projects)PHP Training (5 Courses, 3 Project)Windows 10 Training (4 Courses, 4+ Projects)PL SQL Training (4 Courses, 2+ Projects)Oracle Training (14 Courses, 8+ Projects)

Structure of creating the procedure

Here, the owner means the database name, and to pass the parameter we need to add the ‘@’ symbol.

CREATE PROCEDURE <owner>.<procedure name>
<param> <datatype>
AS
<Body>
Execute a Stored Procedure
Exec is the command for the execution of the procedure.
EXEC Procedure_name

Comparison between Stored procedure and Functions

Stored procedures and functions can be used to achieve the same functionality. We can customize both procedures as well as functions. The difference between them is functions are designed to send their output to Transact SQL and stored procedures are designed to return output to a query as well as they can return output to T-SQL also. Applications can have input from the stored procedures, while user-defined functions can return table variables and they are not privileged to change the environment settings neither operating system environment.

Database Company

Company Category Item
Tarle edibles cookies
Tarle edibles chips
Lakme cosmetics lipstick
Brisket grocery Flax seeds
Brisket edibles biscuits
Brisket grocery oil
ole cosmetics Nail polish
ole cosmetics Hairbrush

Stored Procedure Example

The following SQL statement creates a stored procedure that selects the company

CREATE PROCEDURE SelectAllExample
AS
SELECT * FROM company GO;

Execute the stored procedure above as follows:

Executing the above-stored procedure we can use the following command:

EXEC SelectAllExample;

Stored Procedure Example With One Parameter

The following SQL statement creates a stored procedure with a single parameter to be passed that selects a company with an item as a parameter

CREATE PROCEDURE SelectAllExample @item nvarchar(30)
AS
SELECT * FROM company WHERE item = @item GO;

Execute the stored procedure above as follows:

To execute the above-stored procedure with one parameter we can use the following command:

EXEC SelectAllExample item = "cookies";

Stored Procedure Example With Multiple Parameters

The following SQL statement creates a stored procedure with multiple parameters that select company with item and category as a parameter

CREATE PROCEDURE SelectAllExample @item nvarchar(30), @ category nvarchar(10)
AS SELECT * FROM company WHERE item = @ item AND category = @ category GO;

Execute the stored procedure above as follows:

To execute the above-stored procedure with multiple parameters we can use the following command we need to pass the multiple parameters with comma separated: EXEC SelectAllExample item =”cookies”, category =”edibles”;

Advantages and Disadvantages of Procedure in SQL

Below are the advantages and disadvantages:

Advantages

Let us see some of the advantages:

  • Application improvement can be achieved by procedures. If a procedure is called multiple times in a single application, then the compiled version of the procedure is utilized.
  • Traffic between the database and the application can be reduced as the big statements are already fed into the database and we don’t have to send it again and again.
  • Using procedures code reusability can be achieved, similar to the functions and methods work in the java and other programming languages.

Disadvantages

Below are the disadvantages:

  • Stored Procedures consumes a lot of memory. It is the duty of a database administrator has to decide an upper bound to how many stored procedures are feasible for a particular application.
  • Stored procedures cannot be debugged using MySQL.

Conclusion – What is Procedure in SQL

  • Simple non-select statements including DML statements like Insert and delete, also DDL statements such as Drop and Create can be included in the stored programs.
  • Memory consumption is more but the seek rate can be reduced and code reusability is beneficial.
  • Stored procedures can be implemented with or without variables and can be executed by passing the parameters if required.

Recommended Articles

This has been a guide to What is Procedure in SQL. Here we have discussed the structure, examples, advantages, and disadvantages of the procedure in SQL. You may also have a look at the following articles to learn more –

  1. What is SQL Server?
  2. What is MySQL Database
  3. MySQL OpenSource
  4. What is SQL

SQL Training Program (7 Courses, 8+ Projects)

7 Online Courses

8 Hands-on Projects

73+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Advanced
    • SQL Formatter
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL INSTR()
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
    • SQL Super Key
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • SQL Window Functions
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • ANY in SQL
    • LIKE Query in SQL
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL MOD()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Server Permission
    • T-SQL INSERT
    • SQL Ranking Function
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - SQL Training Program (7 Courses, 8+ Projects) Learn More