EX NO: 5 (a)
Creation of simple PL(Procedural Language)/SQL program
Stored Procedure is a set of statements, which allow ease and flexibility for a programmer because stored procedure is easy to execute than reissuing the number of individual SQL statements. Stored procedure can call another stored procedure also. Stored Procedure can very useful where multiple client applications are written in different languages or it can be work on different platforms but they need to perform the same database operations. Store procedure can improve the performance because by using the stored procedure less information needs to be sent between the server and the client. It increase the load on the database server because less work is done on the client side and much work is done on the server side.
CREATE PROCEDURE Syntax
CREATE PROCEDURE proc_name ([proc_parameter[......]]) routine_body Example :
mysql> delimiter // mysql> CREATE PROCEDURE Sproc(OUT p1 INT) -> SELECT COUNT(*) INTO p1 FROM Emp; -> // Query OK, 0 rows affected (0.21 sec) mysql> delimiter ; mysql> CALL Sproc(@a); Query OK, 0 rows affected (0.12 sec) mysql> select @a; +------+ | @a | +------+ | 5 | +------+ 1 row in set (0.00 sec)
EX NO: 5 (b)
Exception Handling
Some conditions needs specific handling and these conditions can be related to errors or may be general flow control inside a routine. Handlers are the methods of handling conditions that need to be dealt with. Example 1: mysql> delimiter // mysql> CREATE PROCEDURE hproc(OUT p VARCHAR(35)) -> BEGIN -> DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET
@b=' With Errors';
-> INSERT INTO Emp VALUES(1,'AAA','Delhi','Manager',20000,698); -> SET p=CONCAT('Cannot Insert ',@b); -> END -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> CALL hproc(@c); Query OK, 0 rows affected (0.00 sec) mysql> SELECT @c; +---------------------------+ | @c | Cannot Insert With Errors| +---------------------------+ 1 row in set (0.00 sec) | +---------------------------+
Example 2:
mysql> delimiter //
mysql> CREATE PROCEDURE hproc(OUT p VARCHAR(35)) -> BEGIN -> DECLARE CONTINUE HANDLER FOR 1062 SET @b=' With Error 1062'; -> DECLARE CONTINUE HANDLER FOR 1048 SET @b=' With Error 1048'; -> INSERT INTO Emp VALUES(1,'AAA','Delhi','Manager',20000,698); -> SET p=CONCAT('Cannot Insert',@b); -> END -> // Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ; mysql> CALL hproc(@c); Query OK, 0 rows affected (0.00 sec) mysql> SELECT @c;
+--------------------------------+ | @c | +--------------------------------+ | Cannot Insert With Error 1062 | +--------------------------------+ 1 row in set (0.01 sec)
mysql> DROP PROCEDURE hproc; Query OK, 0 rows affected (0.09 sec)
mysql> delimiter // mysql> CREATE PROCEDURE hproc(OUT p VARCHAR(35)) -> BEGIN -> DECLARE CONTINUE HANDLER FOR 1062 SET @b=' With Error 1062'; -> DECLARE CONTINUE HANDLER FOR 1048 SET @b=' With Error 1048'; -> INSERT INTO Emp VALUES(11,NULL,'Delhi','Manager',20000,698); -> SET p=CONCAT('Cannot Insert',@b); -> END -> // Query OK, 0 rows affected (0.09 sec)
mysql> delimiter ; mysql> CALL hproc(@c); Query OK, 0 rows affected (0.04 sec) mysql> SELECT @c;
+--------------------------------+ | @c | +--------------------------------+ | Cannot Insert With Error 1048 | +--------------------------------+ 1 row in set (0.03 sec)