How to Create and Call a Stored Procedure in SQL?
Last Updated :
25 Oct, 2021
With this article, we will learn how to Create and Call a Stored Procedure in SQL. For this article, we are going to use MSSQL as our database server.
What is a Stored Procedure?
A stored procedure is a pre-written SQL query that can be called multiple times and will run as the same. Like we can create a Stored procedure for Insert, select, update in SQL database. We can also pass parameters to the Stored procedures. So, we will create a database first:
Step 1: Creating Database
Query:
CREATE DATABASE GFG
Step 2: Using Database
Query:
USE GFG
Step 3: Create a table
Query:
CREATE TABLE gfgTutorial(
id integer,
Name varchar(20)
)
Step 4: Describe the table
Query:
sp_help 'dbo.gfgTutorial'
Output:
Created Table schemaStep 5: Insert some data into the table
Query:
INSERT INTO [dbo].[gfgTutorial]
([id]
,[Name])
VALUES
(1, 'Devesh')
GO
INSERT INTO [dbo].[gfgTutorial]
([id]
,[Name])
VALUES
(2, 'Geeks')
GO
INSERT INTO [dbo].[gfgTutorial]
([id]
,[Name])
VALUES
(3, 'For')
GO
INSERT INTO [dbo].[gfgTutorial]
([id]
,[Name])
VALUES
(4, 'Geeks')
GO
INSERT INTO [dbo].[gfgTutorial]E
([id]
,[Name])
VALUES
(5, 'GFG')
GO
Step 6: Create a Stored procedure for Select all the rows from a table
Query:
CREATE PROCEDURE select_all_data
AS
SELECT * FROM gfgTutorial
GO;
Output:
Successfully created the stored procedureExecute Stored procedure select_all_data
Query:
EXEC select_all_data
Output:
Executing stored procedure to select all dataNow we have seen how to create a basic stored procedure now let's see how to create the parameterized stored procedure
Step 1: Create a parameterized stored procedure to insert data in the table
Query:
CREATE PROCEDURE insertData
@Name varchar(30), @id varchar(30)
AS
INSERT INTO gfgTutorial VALUES(@id, @Name)
GO
Step 2: Execute stored procedure
Query:
EXEC insertData @Name = 'Inserted Name', @id = 6
Data insertion successfulCheck the data is inserted or not.
Data is inserted by the stored procedure.
Similar Reads
How to SELECT FROM Stored Procedure in SQL Stored procedures are precompiled SQL queries stored in the database that encapsulate logic and can accept parameters, perform operations and return results. They are widely used in SQL for encapsulating reusable logic, improving performance and enhancing security. In this article, weâll explore how
4 min read
How to Use Stored Procedure in SQLite? SQLite is a popular, lightweight, self-contained, serverless, and open-source relational database management system (RDBMS) that is widely used in various applications. SQLite does not directly support stored procedures like other database management systems (DBMS) such as MySQL or PostgreSQL. To ac
4 min read
How to Create and Use Stored Procedures in MySQL with Node.js? Stored procedures in MySQL are very useful in the following ways Regarding the encapsulation of business logic within a database. They can be run multiple times and do not cause a large load on the client-server connection. In this tutorial, we will learn how to create and use stored procedures in M
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer? A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again.
2 min read
How to Modify a Stored Procedure in SQL Server? In this article, we will learn to modify the created stored procedure in MS SQL.You can modify the Stored Procedure in two ways. one is by using a client called SSMS and other way is by using T-SQL statements + SSMS in MS SQL Server. Method 1: Using SQL Server Management Studio (SSMS) to Modify the
3 min read
How to Drop Procedure in SQL In SQL, stored procedures are used to encapsulate complex logic and queries. However, there may come a time when we need to remove or delete a stored procedure from a database. In this article, we will cover the various methods for dropping a stored procedure in SQL along with examples and explanati
3 min read