Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql procedures #485

Merged
merged 17 commits into from
Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions content/sql/concepts/procedures/procedures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
Title: 'Procedures'
Description: 'Procedures are blocks of SQL code that are saved in a database and can be executed repeatedly on demand.'
Subjects:
- 'Data Science'
Tags:
- 'Database'
- 'MySQL'
CatalogContent:
- 'learn-sql'
- 'paths/analyze-data-with-sql'
---

Procedures are blocks of SQL code that are saved in a database and can be executed repeatedly on demand. Also referred to as stored procedures — they can be defined with parameters to be used within the body of the procedure, produce an output value, or both.

The syntax for creating a procedure varies depending upon the type of database management system (DBMS) being used. Below is an example procedure defined and executed with [MySQL](https://www.mysql.com/) that accepts two parameters and returns an output value.

## MySQL Example

```sql
# Set the default delimiter so the procedure can include semicolons
DELIMITER //

# Create the procedure
CREATE PROCEDURE add_int (IN x INT, IN y INT, OUT z INT)
BEGIN
SELECT x + y INTO z;
END //

# Set the delimiter back to the default
DELIMITER ;

# Execute the stored procedure
SSwiniarski marked this conversation as resolved.
Show resolved Hide resolved
CALL add_int (5, 3, @z);
# Return the output parameter
SELECT @z AS Result;
```

Output:

| Result |
| ------ |
| 8 |