0% found this document useful (0 votes)
1 views

Experiment 6 dbms (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Experiment 6 dbms (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment - 6

Objective- Creating Procedure and Functions in MySQL/Oracle

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.

Call a stored procedure:


We can use the CALL statement to call a stored procedure. This statement returns the values to its
caller through its parameters (IN, OUT, or INOUT).
The following syntax is used to call the stored procedure in MySQL:
sql
CALL procedure_name ( parameter(s) );
Example:
Let us understand how to create a procedure in MySQL through example. First, we need to select a
database that will store the newly created procedure. We can select the database using the below
statement:
sql
mysql> USE database_name;

Procedure without Parameter:

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 &&

CREATE PROCEDURE get_student (IN var1 INT)

BEGIN

SELECT * FROM student_info LIMIT var1;

SELECT COUNT(stud_code) AS Total_Student FROM student_info;

END &&

DELIMITER ;

After successful execution, we can call the procedure as follows:


mysql> CALL get_student(4);
We will get the below output:
Procedures with OUT Parameter
In this procedure, we have used the OUT parameter as the 'highestmark' of integer type. Its body part
fetches the maximum marks from the table using a MAX() function. See the procedure code:
sql
Copy code
DELIMITER &&
CREATE PROCEDURE display_max_mark (OUT highestmark INT)
BEGIN
SELECT MAX(marks) INTO highestmark FROM student_info;
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:

mysql> SET @M-3;


mysql> CALL display_marks(@M);
mysql> SELECT @M
We will get the below output:

You might also like