Module 9 SQL Stored Procedure
2026-04-09 00:34
Tags: #ADV_DBMS
Author: Duke Hsu
1.0 Key Concepts
1.1.0 Stored Procedure
- A stored procedure is a prepared SQL code that you can save , so the code can be reused over and over again .
- A stored procedure is similar to a function or method in a programming language. It can accept parameters, execute a set of operations, and return results.
1.1.1 Keywords in Stored Procedure
A. Definition and Creation
- CREATE PROCEDURE / ALTER PROCEDURE
-
The primary command used to define a new procedure in the database
-
AS
-
Separates the procedure header(name and parameters) from its body.
-
BEGIN ......END
-
Delimits the block of SQL statements that make up the procedures logic.
-
DROP PROCEDURE
- Removing procedure
B. Execution and Control
- EXEC(or EXECUTE)
-
Used to run a stored procedure in SQL Server.
-
OUTPUT
- Indicates that a parameter is used to return a value back to the caller.
2.0 Stored Procedure and Example
2.1.0 Stored Procedure Syntax (SQL Server)
Creating a Stored Procedure
Syntax:
1 2 3 4 5 6 7 8 9 | |
Execute a Stored Procedure
Syntax:
1 | |
Drop a Stored Procedure
Syntax:
With out IF EXISTS
1 | |
With IF EXISTS
1 | |
IF EXISTS
To ensure thatDROP PROCEDURE does not return an error, if the procedure is missing, add the IF EXISTS clause.
2.1.1 Stored Procedure Example
Create a Stored Procedure that displays the annual salary of an employee.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Using SQL Stored Procedure
1 | |
References
Module 9 PPT