What is a procedure?
- SQL course
- Advanced
- Lesson 36/81
- 4 min
Learn the server-side stored routine.
What is a procedure?
A stored procedure is SQL code stored in the database. Called by name.
CREATE PROCEDURE get_active()
LANGUAGE SQL
AS $$
SELECT * FROM customers WHERE status = 'active';
$$;This query returns active customers.
The procedure get_active is created. All columns are selected from customers. WHERE keeps only the rows where status = 'active' holds.
What is a procedure? — details
A procedure is called repeatedly. It does complex operations in one step.
For example, you bundle creating an order, decreasing stock and writing a log into one procedure; the application just triggers it with CALL.
Practice What is a procedure?
Create a procedure named get_active.
Starter code
-- create procedure
CREATESample solution
CREATE PROCEDURE get_active()
LANGUAGE SQL
AS $$
SELECT * FROM customers WHERE status = 'active';
$$;Expected output
Procedure oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Where is a stored procedure stored?
- In a view
- In a file
- In an index
- In the database Correct answer
Explanation: A procedure is stored in the database.
2 Add PROCEDURE
CREATE ____ get_active() AS $$
SELECT * FROM customers WHERE status = 'active';
$$;
CREATE PROCEDURE get_active() AS $$
SELECT * FROM customers WHERE status = 'active';
$$;
Explanation: PROCEDURE is the right answer.
3 What is a procedure: match the terms
| Term | Meaning |
|---|---|
procedure |
procedure |
CREATE |
create |
CALL |
call |
database |
store |