Stored Procedure
Stored Procedure
STORED
PROCEDURE
IF
STATEMENT
The ‘IF’ statement allows you to
STATEMENT conditionally execute SQL
statements based on a specified
condition.
4. The ‘DELIMITER’ is temporarily changed to ‘//’ to allow the use of semicolons within the
procedure. After defining the procedure, we reset the delimiter back to ‘;’.
You can then call this stored procedure by passing an employee ID as an argument to increase the
employee's salary based on the conditions specified in the ‘IF’ statement.
CASE
STATEMENT
A stored procedure is a set of SQL statements that
can be executed as a single unit. You can use the
STATEMENT CASE statement within a stored procedure to
conditionally execute different blocks of SQL code
based on specified conditions.
Basic Syntax of CASE Statement:
CASE
1. ‘DELIMITER’: This is used to change the default statement
EXPLANATION OF delimiter from a semicolon (’;’) to ‘//’ or another character. It
allows you to include semicolons within the body of the stored
procedure without them being treated as the end of the procedure
definition.
COMPONENTS
2. ‘CREATE PROCEDURE YourProcedureName()’: This line declares the
beginning of the stored procedure definition. Replace
‘YourProcedureName’ with the name you want for your stored
procedure. You can also specify input parameters within the
parentheses if your procedure requires them.
3. ‘BEGIN’ and ‘END’: These keywords enclose the body of the stored
procedure. All the SQL statements and logic for your procedure go
between these keywords.
4. ‘DECLARE variable_name datatype;’: You can use the ‘DECLARE’
statement to declare variables that can be used within the stored
procedure. Replace ‘variable_name’ with the name of your variable
and datatype with the ‘data type’ of the variable.
5. ‘CASE’: This keyword marks the beginning of the ‘CASE’ statement.
6. ‘WHEN condition1 THEN’: In this section, you specify the first
condition to be checked. If this condition is true, the code block
following it will be executed.
7. ‘-- Code block to execute if condition1 is true’: This is where
you place the SQL statements to be executed if ‘condition1’ is
true.
8. ‘ELSE’: This section is optional and is executed if none of the
EXPLANATION OF conditions specified in the ‘WHEN’ clauses is true.
9. ’-- Code block to execute if none of the conditions are true’:
Here, you include SQL statements to be executed if none of the
conditions are met.
COMPONENTS
10. ‘END CASE;’: This marks the end of the ‘CASE’ statement.
11. Additional SQL statements: You can include any additional SQL
statements after the ‘CASE’ statement as needed for your stored
procedure.
12. ‘DELIMITER ;’: This sets the delimiter back to the default
semicolon.
Once you've created your stored procedure, you can call it by using
the CALL statement followed by the procedure name and any required
arguments.
Once you've created your stored procedure, you can call it by using the CALL statement followed by
the procedure name and any required arguments. For example:
This will execute the stored procedure you've defined with its
associated logic.
EXAMPLE:
In this example:
We create a stored procedure named
‘CalculateGrade’ that takes two input
parameters: ‘student_id’ and ‘score’.
Inside the procedure, we declare a ‘grade’
variable of type ‘CHAR(1)’ to store the
calculated grade.
We use a ‘CASE’ statement to evaluate the
‘score’ and assign the appropriate letter grade
('A', 'B', 'C', 'D', or 'F').
After determining the grade, we insert a record
into a hypothetical "Grades" table, associating
the student ID, score, and calculated grade.
Finally, we return a message that displays the
student's ID and their calculated grade.
This would calculate the grade for a student with ID 1 who scored 85 and insert the corresponding
record into the "Grades" table. The stored procedure will also return a message indicating the
result.
LOOP
LOOP
MySQL supports
several loop
constructs within
stored
procedures, The basic syntax is as follows:
including WHILE,
REPEAT, and LOOP.
WHILE LOOP EXAMPLE:
WHILE Loop:
The WHILE loop executes a block of statements as long as a specified condition is true.
This stored procedure also prints numbers from 1 to 5, but it uses a REPEAT loop.
INFINITE LOOP EXAMPLE:
LOOP: The LOOP creates an infinite loop that must be exited using the LEAVE statement.
This stored procedure also prints numbers from 1 to 5 but uses an infinite LOOP that is
exited when the counter exceeds 5 by using the LEAVE statement.
LEAVE
STATEMENT
In MySQL, you can create stored procedures to
STATEMENT
encapsulate a series of SQL statements into a reusable
and modular unit. The ‘LEAVE’ statement is used within a
stored procedure to exit the procedure prematurely. It
is similar to the ‘RETURN’ statement, but it's often
used for more complex control flow scenarios.
This will execute the stored procedure for the student with ‘student_id’ 1, and it will
either return the average grade or a message indicating that no subjects were found for
the student.