L 23 Introduction To PLSQL and Stored Procedures
L 23 Introduction To PLSQL and Stored Procedures
and Web
(15B11CI312)
Database Systems and Web
❑ Direct call can also be made from external programming language calls to database.
❑Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.
PL/SQL Features
❑ Tightly integrated with SQL.
❑ Stored program is a computer program or a set of instructions associated with a name which are
stored within, and executes within, the database server.
DELIMITER delimiter_character
MySQL Delimiter
SELECT * FROM products; DELIMITER // To change the
SELECT * FROM customers // delimiter to //
SELECT * FROM customers; SELECT * FROM products //
DELIMITER ; To change the
delimiter back to
semicolon
Note: Output of both the programs will be same
MySQL Delimiter
DELIMITER $$ ❑ Change the default delimiter to $$
CREATE PROCEDURE sp_name()
BEGIN ❑ Use (;) in the body of the stored procedure
- - statements and $$ after the END keyword to end the stored
END $$ procedure.
DELIMITER ;
❑ Change the default delimiter back to a
semicolon (;)
Create and Execute a procedure
❑ A procedure can return one or more than one value through parameters or may not return at all.
❑ OUT: No value is supplied to the routine (it is assumed to be NULL), but it can be modified
inside the routine, and it is available outside the routine. “ @”
❑ INOUT: The characteristics of both IN and OUT parameters. A value can be passed to the
routine, modified there as well as passed back again.
Example
Create and call a procedure to extract the details of products from Products table.
DELIMITER //
CREATE PROCEDURE GetAllProducts()
CALL GetAllProducts();
BEGIN
SELECT * FROM Products;
END //
DELIMITER ;
Drop Procedure
DROP PROCEDURE [IF EXISTS] stored_procedure_name;
❑ While dropping a procedure that does not exist by using the IF EXISTS option, MySQL issues a
warning .
DECLARE x, y INT
DEFAULT 0;
Assigning variables
Example:
There is one more statement “SELECT INTO” to assign the result of a query to a variable.
loop_label: LOOP
IF x > 10 THEN
LEAVE loop_label;
END IF;
SET x = x + 1;
IF (x mod 2) THEN
ITERATE loop_label;
ELSE
SET str = CONCAT(str,x,',');
END IF;
END LOOP;
SELECT str;
END$$
Create a stored procedure that constructs a string from the
even numbers between 1 to 10
DELIMITER $$
CREATE PROCEDURE WhileLoopExample()
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
WHILE x<=10 DO
IF (x mod 2) THEN
SET x = x + 1;
ELSE
SET str = CONCAT(str,x,',');
SET x = x + 1;
END IF;
END WHILE;
SELECT str;
END$$
DELIMITER ;
Create a stored procedure that constructs a string from the
even numbers between 1 to 10
DELIMITER $$
CREATE PROCEDURE RepeatLoopExample()
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
REPEAT
IF (x mod 2) THEN
SET x = x + 1;
ELSE
SET str = CONCAT(str,x,',');
SET x = x + 1;
END IF;
UNTILL x<11
END REPEAT;
SELECT str;
END$$
DELIMITER ;
Error Handling in Stored Procedures
❑ CONTINUE : The execution of the enclosing code block ( BEGIN … END ) continues.
❑ EXIT : The execution of the enclosing code block, where the handler is declared, terminates.
References
1. https://www.oracletutorial.com/plsql-tutorial
2. https://www.mysqltutorial.org/
3. https://www.javatpoint.com/mysql-procedure
4. https://www.w3resource.com/mysql/mysql-procedure.php
5. Murach's MySQL, 3rd edition by Joel Murach.