0% found this document useful (0 votes)
48 views5 pages

PL/SQL Procedures and Functions Guide

Uploaded by

SUMEDH PATIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

PL/SQL Procedures and Functions Guide

Uploaded by

SUMEDH PATIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name : Sanket Raju Nagade

Roll No : SI-28
Subject : DBMS
Assignment 06
Write and execute PL/SQL stored procedure and function to perform a suitable task on the
database. Demonstrate to use.

1. A procedure to display stu_id and student.

mysql> use sanket18;


Database changed
mysql> create table student
-> (stu_id int(5) not null,
-> student_name varchar(10),
-> DOB date,
-> primary key(stu_id));
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> insert into student values(1, 'Sai', 20040912);


Query OK, 1 row affected (0.01 sec)

mysql> insert into student values(2, 'Sanket', 20041214);


Query OK, 1 row affected (0.01 sec)

mysql> insert into student values(3, 'Savim', 20040818), (4, 'Vinit', 20040321);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from student;


+ + + +
| stu_id | student_name | DOB |
+ + + +
| 1 | Sai | 2004-09-12 |
| 2 | Sanket | 2005-03-14 |
| 3 | Savim | 2004-07-08 |
| 4 | Vinit | 2004-08-08 |

+ + +
+ 4 rows in set (0.00 sec)

mysql> delimiter $
mysql> create procedure my_pro()
-> select stu_id, student_name from student
-> $
Query OK, 0 rows affected (0.08 sec)

mysql> call mypro()$


ERROR 1305 (42000): PROCEDURE [Link] does not exist
mysql> call my_pro()$
+ + +
| stu_id | student_name |
+ + +
| 1 | Sai |
| 2 | Sanket |
| 3 | Savim |
| 4 | Vinit |

+ + +
4 rows in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> create procedure stud(IN id INT(5), OUT name varchar(10))


-> begin
-> select student_name into name
-> from student
-> where stu_id = id;
-> end$
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> call stud(2, @x)$


Query OK, 1 row affected (0.01 sec)

mysql> call stud(2, @x)


-> $
Query OK, 1 row affected (0.00 sec)

mysql> select @x$


+ +
| @x |
+ +
| Sanket |
+ +
1 row in set (0.00 sec)

mysql> create procedure cleanup()


-> delete from student;
-> $
Query OK, 0 rows affected (0.01 sec)

mysql> call cleanup()$


Query OK, 4 rows affected (0.00 sec)

mysql> select * from student;$


Empty set (0.00 sec)

2. A procedure to find maximum number.


mysql> delimiter #
mysql> create procedure findmax(IN a int, IN b int)
-> begin
-> declare maxx int;
-> if a>b then set maxx = a;
-> else set maxx = b;
-> end if;
-> select maxx as 'Maximum Number';
-> end#
Query OK, 0 rows affected (0.01 sec)

mysql> call findmax(14, 9);


-> #
+ +
| Maximum Number |
+ +
| 14 |
+ +
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

3. Function to find odd or even number.


mysql> delimiter @
mysql> create function oddeve(a int)
-> returns varchar(10)
-> deterministic
-> begin
-> declare result varchar(10);
-> if a%2=0 then
-> set result = 'Even';
-> else
-> set result = 'Odd';
-> end if;
-> return result;
-> end@
Query OK, 0 rows affected (0.01 sec)

mysql> select oddeve(14);


-> @
+ +
| oddeve(14) |
+ +
| Even |
+ +
1 row in set (0.01 sec)

mysql> select oddeve(7);


-> @
+ +
| oddeve(7) |
+ +
| Odd |
+ +
1 row in set (0.00 sec)

Common questions

Powered by AI

The DELIMITER command is significant in MySQL when defining stored procedures and functions because it instructs the MySQL client on how to interpret statement boundaries. By changing the delimiter, it allows the use of semicolons within stored procedure definitions without confusing them with the end of the procedure. For instance, DELIMITER $ is used in Source 1 to define the end of the procedure and avoid premature termination of the SQL command .

IN parameters pass input data to the procedure, while OUT parameters allow the procedure to return data to the caller. In the 'stud' procedure from Source 1, 'id' is an IN parameter accepting a student ID, and 'name' is an OUT parameter returning the student's name. IN parameters enhance flexibility by allowing dynamic input, while OUT parameters are crucial for returning computed or retrieved values efficiently without needing additional queries .

To create a stored procedure for selecting student details, you begin by declaring the procedure using the 'CREATE PROCEDURE' statement, specifying the SQL query logic within it. In Source 1, 'my_pro' procedure is created to select 'stu_id' and 'student_name' from the 'student' table. No specific error handling is implemented within this basic procedure; however, MySQL provides mechanisms like DECLARE ... HANDLER for error handling .

Stored procedures can automate repetitive tasks by predefining logic that operates on the database. In Source 1, various stored procedures are demonstrated: one procedure displays student IDs and names, another finds the maximum of two numbers, and a third uses input-output parameters to retrieve a student's name based on ID. Using stored procedures offers benefits like reduced network traffic, enforcement of data integrity, and encapsulation of business logic within the database .

A DELETE statement within a stored procedure is beneficial when orchestrating complex, conditional, or repetitive delete operations as part of a larger transactional workflow. It maintains database integrity and performance by ensuring that only authorized deletions occur. In Source 1, the 'cleanup' procedure uses DELETE to remove all records from the 'student' table, showcasing a full table cleanup scenario .

In the 'stud' procedure, parameter specification determines how data is passed between the caller and the procedure. The IN parameter 'id' allows the caller to specify which student's name should be retrieved, while the OUT parameter 'name' allows the procedure to return the result. This dual parameter structure enables flexible interaction and ensures that relevant data is both consumed and provided by the procedure .

The 'oddeve' function determines parity by checking the remainder of the integer 'a' when divided by 2 using the modulus operator (%). If the result is zero, 'a' is even; otherwise, it is odd. The function returns a string: 'Even' or 'Odd', based on the parity of the input integer .

The 'findmax' stored procedure determines the maximum of two numbers using an IF statement within a BEGIN-END block. It declares an integer 'maxx', then checks if 'a' is greater than 'b'; if true, 'maxx' is set to 'a', otherwise to 'b'. Finally, it selects and displays 'maxx' as the 'Maximum Number' .

Deterministic functions in databases guarantee the same output for the same set of input values, facilitating optimization by allowing caching of results. The 'oddeve' function is marked deterministic as it consistently returns 'Even' or 'Odd' based on the parity of the input integer, ensuring reliable and repeatable results every time it's executed with the same input .

The error that occurred was 'PROCEDURE dbms.mypro does not exist,' indicating that the stored procedure 'mypro' was never created or there was a typographical error in the name. It was resolved by correctly calling the procedure using 'call my_pro()', the name with which it was originally created .

You might also like