Description
The SQL keyword PROCEDURE is used to create a stored procedure.
- A stored procedure is a prepared SQL query, that can be saved and reused anytime.
- A stored procedure is usually created for the most frequently used SQL queries, which we may execute it later.
Create Stored Procedure
The SQL keyword CREATE PROCEDURE is used to create a stored procedure.
The below SQL creates a stored procedure with a SELECT statement, that returns all customers.
CREATE PROCEDURE all_customers
AS
SELECT * FROM customers
GO;
Execute Stored Procedure
After the stored procedure is created, it can be executed using the EXEC command.
The below SQL executes an existing stored procedure.
EXEC all_customers;