Experiment 6 dbms (1)
Experiment 6 dbms (1)
A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over
again. So if you have an SQL query that you write over and over again, save it as a stored procedure,
and then just call it to execute it.You can also pass parameters to a stored procedure, so that the stored
procedure can act based on the parameter value(s) that is passed.
Stored Procedure Syntax
1. CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
2. Execute a Stored Procedure
EXEC procedure_name;
The procedure syntax has the following parameters:
MySQL procedure parameter has one of three modes:
IN parameter
It is the default mode. It takes a parameter as input, such as an attribute. When we define it, the calling
program has to pass an argument to the stored procedure. This parameter's value is always protected.
OUT parameters
It is used to pass a parameter as output. Its value can be changed inside the stored procedure, and the
changed (new) value is passed back to the calling program. It is noted that a procedure cannot access
the OUT parameter's initial value when it starts.
INOUT parameters
It is a combination of IN and OUT parameters. It means the calling program can pass the argument,
and the procedure can modify the INOUT parameter, and then passes the new value back to the
calling program.
Suppose we want to display all records of this table whose marks are greater than 70 and count all the
table rows. The following code creates a procedure named
get_merit_student:-
DELIMITER &&
CREATE PROCEDURE get_merit_student ()
BEGIN
SELECT * FROM student_info WHERE marks > 70;
SELECT COUNT(stud_code) AS Total_Student FROM student_info;
END &&
DELIMITER ;
Output get_merit_student :-
Procedures with IN Parameter
In this procedure, we have used the IN parameter as var1 of integer type to accept a number from
users. Its body part fetches the records from the table using a SELECT statement and returns only
those rows that will be supplied by the user. It also returns the total number of rows of the specified
table. See the procedure code:
DELIMITER &&
BEGIN
END &&
DELIMITER ;
This procedure's parameter will get the highest marks from the student_info table. When we call the
procedure, the OUT parameter tells the database systems that its value goes out from the procedures.
Now, we will pass its value to a session variable @M in the CALL statement as follows:
less
Copy code
mysql> CALL display_max_mark(@M);
mysql> SELECT @M;
Procedures with INOUT Parameter:-
In this procedure, we have used the INOUT parameter as 'var1' of integer type. Its body part first
fetches the marks from the table with the specified id and then stores it into the same variable var1.
The var1 first acts as the IN parameter and then OUT parameter.
In this procedure, we have used the INOUT parameter as 'var1' of integer type. Its body part first
fetches the marks from the table with the specified id and then stores it into the same variable var1.
The var1 first acts as the IN parameter and then OUT parameter.
Therefore, we can call it the INOUT parameter mode. See the procedure code:
sql
Copy code
DELIMITER &&
CREATE PROCEDURE display_marks (INOUT var1 INT)
BEGIN
SELECT marks INTO var1 FROM student_info WHERE stud_id = var1;
END &&
DELIMITER ;
After successful execution, we can call the procedure as follows: