{"id":253,"date":"2018-07-13T06:51:28","date_gmt":"2018-07-13T06:51:28","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=253"},"modified":"2018-12-07T11:35:21","modified_gmt":"2018-12-07T11:35:21","slug":"stored-procedures-in-mysql","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/stored-procedures-in-mysql\/","title":{"rendered":"Stored Procedures in MYSQL"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/SWWTiRw18uY\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 A stored procedure or a function is a named Pl\/SQL block which resides in the Database engine\u2019s tables. A stored procedure can be invoked by other procedure, triggers or by other applications like PHP, java etc.<\/p>\r\n<p style=\"text-align: justify\">\u2022 It is a logically grouped set of SQL and Pl\/SQL statements which are written to perform a specific task.<\/p>\r\n<p style=\"text-align: justify\">\u2022 Procedures and functions are also referred to as Database objects and such objects can be invoked or called by any Pl\/SQL block within the application.<\/p>\r\n<p style=\"text-align: justify\">\u2022 The initial versions of MySQL did not have support for stored procedures, functions or triggers. It was only after version 5.0 that support for this objects was added to increase reusability.<\/p>\r\n<p style=\"text-align: justify\">\u2022 It is like defining an API for our application which helps in enhancing reusability in different applications<\/p>\r\n\u2022 Before they are actually stored, MySQL engine parses and compiles the procedure.\r\n\r\n&nbsp;\r\n\r\n<strong>Why Stored Procedures<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Stored procedures provide security as the DBA can grant permission to the applications to access the stored procedure without giving permissions on the underlying table.<\/p>\r\n<p style=\"text-align: justify\">\u2022 Stored procedures enhances reusability so the developers can use once written code and multiple resources are not wasted for single task.<\/p>\r\n<p style=\"text-align: justify\">\u2022 Stored procedures are invoked by some calling routine which pass only the name and parameters hence reducing database traffic , which otherwise has to send multiple sql statements<\/p>\r\n<p style=\"text-align: justify\">\u2022 The caching and buffering of stored procedures in MySQL is on demand , they are compiled on demand. And after compilation MySQL puts it in cache<\/p>\r\n&nbsp;\r\n\r\n<strong>Limitations<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 Expert skill set required\r\n\r\n\u2022 MySQL does not provide with debugging facilities\r\n<p style=\"text-align: justify\">\u2022 Memory consumption will be more if many procedures are used or many logical operations in a procedure.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 <\/strong>\r\n\r\n<strong>\u00a0 Types of Parameters in Stored Procedure<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Procedures can also be made dynamic. The dynamism for procedures can be achieved by passing parameters before execution. The behavior of the procedure changes depending on the parameters passed.<\/p>\r\n\u2022 The different types of parameters available with stored procedure are\r\n\r\n\u2022 IN : IN is the default parameter passed to the procedure which can be changed in the procedure\r\n\r\n\u2022 OUT : No value is passed to the procedure , but the value is returned out from the procedure.\r\n\r\n\u2022 IN OUT : Here , both value can be accepted and can be returned.\r\n\r\n&nbsp;\r\n\r\n<strong>Structure of Stored Procedure<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 Stored procedure consists of\r\n\r\n\u2022 Declarative part\r\n<p style=\"text-align: justify\">\u2022 The declarative part contains the declarations about variables, constants, cursors, exceptions etc. The scope of these declarations is local to the procedure<\/p>\r\n\u2022 Executable part\r\n\r\n\u2022 The executable part contains the actions procedure is expected to take.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-256 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-119.png\" alt=\"\" width=\"545\" height=\"377\" \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nNow, let us understand different types of examples, with reference to stored procedures.\r\n\r\n&nbsp;\r\n\r\nAll the below code blocks are taken from MySQL editor and displayed here as it is.\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Simple example to fetch data from employee table\r\n\r\n&nbsp;\r\n\r\nDELIMITER $$\r\n\r\nCREATE DEFINER=`root`@`localhost` PROCEDURE `get_sal`()\r\n\r\nBEGIN\r\n\r\nselect name,salary from emp;\r\n\r\nEND\r\n\r\n&nbsp;\r\n\r\nTo run on command prompt SQL &gt; call get_sal();\r\n\r\nThe above statement will display the name and salary from emp table.\r\n\r\n&nbsp;\r\n\r\n2. Example to demonstrate IN parameter\r\n\r\n&nbsp;\r\n\r\nDELIMITER $$\r\n\r\nCREATE DEFINER=`root`@`localhost` PROCEDURE `raise_sal`(emp_no varchar(10), amount int)\r\n\r\nBEGIN\r\n<p style=\"text-align: justify\">UPDATE emp<\/p>\r\n<p style=\"text-align: justify\">SET salary = salary + amount WHERE empid = emp_no;<\/p>\r\n<p style=\"text-align: justify\">END<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the above example, emp_no and amount are passed as IN parameters, and the salary is updated based on the amount provided by the user. By default, parameters are IN.<\/p>\r\n&nbsp;\r\n\r\nTo run on command prompt SQL &gt; call raise_sal(1,100);\r\n\r\n&nbsp;\r\n\r\nThe above statement will update the salary with 100 of empid : 1 , Hence the old value was 5400 which will be updated to 5500.\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">3. Example to demonstrate OUT parameter<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nDELIMITER $$\r\n<p style=\"text-align: justify\">CREATE DEFINER=`root`@`localhost` PROCEDURE `fetch_name`(empno varchar(10), out v_name varchar(10))research<\/p>\r\nBEGIN\r\n\r\nselect name into v_name from emp where empid=empno;\r\n\r\nEND\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The above example demonstrates the use of OUT parameter and IN parameter. Empno is passed as IN parameter and name is returned as OUT parameter.<\/p>\r\n&nbsp;\r\n\r\n4.\u00a0 Example to demonstrate IN OUT parameter\r\n\r\n&nbsp;\r\n\r\nDELIMITER $$\r\n\r\nCREATE DEFINER=`root`@`localhost` PROCEDURE `raise_sal1`(empno varchar(10), Inout amount int)\r\n\r\nBEGIN\r\n\r\ndeclare v_amt int; declare v_sal int;\r\n\r\nselect salary into v_sal from emp where empid = empno; set v_amt = v_sal + amount;\r\n\r\nupdate emp set salary = v_amt where empid = empno; set amount=v_amt;\r\n\r\nEND\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The above example display the functionality of InOUT parameter where the variable amount is accepting the value, it is then processed and the same amount is returned back the value.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">5. Example to demonstrate creating and executing a stored procedure on SQL prompt , Example of IN OUT parameter<\/p>\r\n&nbsp;\r\n\r\nCreating the procedure\r\n\r\nSQL&gt; create procedure square (INOUT P int) set P=P*P;\r\n\r\nTo assign value to the variable SQL&gt; set @number=10;\r\n\r\nTo call the procedure\r\n\r\nSQL&gt; call square (@number);\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">To display variable value<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SQL&gt; select @number ;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">To drop a procedure<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SQL&gt; drop procedure square;<\/span>\r\n\r\n<\/div>\r\n<ul>\r\n \t<li>Procedures allow you to return multiple values, You can separate the parameters with \u201c,\u201d(comma).<\/li>\r\n \t<li>Loops can also be used into Mysql. The three types of loops which exist in MySQL are<\/li>\r\n \t<li>Syntax : While &lt;expression&gt; Do Statements<\/li>\r\n \t<li>End While<\/li>\r\n \t<li>Repeat<\/li>\r\n<\/ul>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nStatements\r\n\r\nUntil &lt;expression&gt;\r\n\r\nEnd Repeat\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">MySQL also provides with Loop statement which executes code repeatedly by using Loop_label e.g. Loop _label: loop. You can use Leave and iterate statements to navigate in the loop.<\/p>\r\n&nbsp;\r\n\r\n6. Example to demonstrate creating and executing a stored procedure on SQL prompt , Example of IN OUT parameter\r\n\r\n&nbsp;\r\n\r\nDELIMITER $$\r\n\r\nCREATE DEFINER=`root`@`localhost` PROCEDURE `test_loop`()\r\n\r\nBEGIN\r\n\r\nDECLARE cntr INT;\r\n\r\nDECLARE str VARCHAR(255);\r\n\r\nSET cntr = 1;\r\n\r\nSET str = '';\r\n\r\nloop_label: LOOP\r\n\r\nIF cntr &gt; 10 THEN\r\n\r\nLEAVE loop_label;\r\n\r\nEND IF;\r\n\r\nSET cntr= cntr + 1;\r\n\r\nIF (cntr mod 2) THEN\r\n\r\nITERATE loop_label;\r\n\r\nELSE\r\n\r\nSET str = CONCAT(str,cntr,',');\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">END IF;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">END LOOP;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SELECT str;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">END<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Output: 2,4,6,8,10<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">THE Procedures which are created can be viewed by issuing the following command<\/p>\r\n&nbsp;\r\n\r\n<strong>Show procedure status<\/strong>\r\n\r\n&nbsp;\r\n\r\nIt displays all the statistics about the procedure\r\n\r\n&nbsp;\r\n\r\n<strong>Stored Functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022\u00a0 Stored functions are the stored programs that return a single value. We make use of stored functions when we want to use some common routines which increases reusability.<\/p>\r\n\u2022\u00a0 But unlike stored procedure, we can use functions with an expression.\r\n\r\n\u2022\u00a0 Functions can have only IN parameters\r\n\r\n\u2022\u00a0 The return statement is compulsory in the function with a valid SQL datatype\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-257 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-120.png\" alt=\"\" width=\"572\" height=\"409\" \/>\r\n\r\n<\/div>\r\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Simple example to fetch data from employee table<\/span><\/div>\r\n<div><\/div>\r\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0DELIMITER $$<\/span><\/div>\r\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0CREATE DEFINER=`root`@`localhost` FUNCTION `get_sal`(empno varchar(10)) RETURNS int(11)<\/span><\/div>\r\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0BEGIN<\/span><\/div>\r\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Declare v_sal int;<\/span><\/div>\r\n<div>\u00a0 \u00a0 \u00a0<span style=\"text-align: initial;font-size: 1em\">select sal into v_sal from emp where empid = empno;<\/span><\/div>\r\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0RETURN v_sal;<\/span><\/div>\r\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0END<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nTo execute on command prompt\r\n\r\nselect empid,get_sal1(1) from emp where empid=1;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The function call is used as part of expression here. The query calls the function and fetches the salary for the empid passed.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The simple example is illustrated to demonstrate the simple understanding of functions, but functions can be used to perform complex calculations and return results from complex queries.<\/p>\r\n\r\n<\/div>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Stored Procedures in MYSQL<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/SWWTiRw18uY\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\nReferences:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers<\/p>\r\n<p style=\"text-align: justify\">2) Learning PHP, MySQL &amp; JavaScript , Robin Nixon , O\u2019Reilly Publications<\/p>\r\n<p style=\"text-align: justify\">3) PHP and MySQL Web Development , Luke Welling , Laura Thomson , Pearson Publications, Fourth Edition<\/p>\r\n<p style=\"text-align: justify\">4) MySQL Documentation : https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/create-procedure.html<\/p>\r\n<p style=\"text-align: justify\">5) mysqltutorial.org<\/p>\r\nAdditional Reading :\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers<\/p>\r\n<p style=\"text-align: justify\">2) MysQL documentation : dev.mysql.com\/doc\/ Practice Questions<\/p>\r\n<p style=\"text-align: justify\">1) Create a Stored Procedure Named DisplayMessage which will display the message \u201cWelcome to E-pg-Pathshala\u201d<\/p>\r\n<p style=\"text-align: justify\">2) Create a Procedure named AddEmp which will add records in emp table.<\/p>\r\n<p style=\"text-align: justify\">3) Create a Procedure DisplayMarks, which will return the total marks of a particular student. (student id will be passed as parameter)<\/p>\r\n<p style=\"text-align: justify\">4) Delete records from emp table based on the empid passed as the parameter. Display number of records deleted.<\/p>\r\n<p style=\"text-align: justify\">5) Create a Procedure Raisesal which will update the employees salary based on following conditions. (you will have to add designation field in emp table)<\/p>\r\n\r\n<ol>\r\n \t<li>If employee is Director : increment will be 20%<\/li>\r\n \t<li>If employee is Manager : increment will be 15%<\/li>\r\n \t<li>If employee is Programmer : increment will be 10%<\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\n6) Create a function to :\r\n<ol>\r\n \t<li>Return the maximum salary for department 10<\/li>\r\n \t<li>Return whether the number passed is prime or not<\/li>\r\n \t<li>Return the total salary of employee after adding 10% DA and deduction 2% tax (to be done for empid passed).<\/li>\r\n<\/ol>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/SWWTiRw18uY\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 A stored procedure or a function is a named Pl\/SQL block which resides in the Database engine\u2019s tables. A stored procedure can be invoked by other procedure, triggers or by other applications like PHP, java etc.<\/p>\n<p style=\"text-align: justify\">\u2022 It is a logically grouped set of SQL and Pl\/SQL statements which are written to perform a specific task.<\/p>\n<p style=\"text-align: justify\">\u2022 Procedures and functions are also referred to as Database objects and such objects can be invoked or called by any Pl\/SQL block within the application.<\/p>\n<p style=\"text-align: justify\">\u2022 The initial versions of MySQL did not have support for stored procedures, functions or triggers. It was only after version 5.0 that support for this objects was added to increase reusability.<\/p>\n<p style=\"text-align: justify\">\u2022 It is like defining an API for our application which helps in enhancing reusability in different applications<\/p>\n<p>\u2022 Before they are actually stored, MySQL engine parses and compiles the procedure.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Why Stored Procedures<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Stored procedures provide security as the DBA can grant permission to the applications to access the stored procedure without giving permissions on the underlying table.<\/p>\n<p style=\"text-align: justify\">\u2022 Stored procedures enhances reusability so the developers can use once written code and multiple resources are not wasted for single task.<\/p>\n<p style=\"text-align: justify\">\u2022 Stored procedures are invoked by some calling routine which pass only the name and parameters hence reducing database traffic , which otherwise has to send multiple sql statements<\/p>\n<p style=\"text-align: justify\">\u2022 The caching and buffering of stored procedures in MySQL is on demand , they are compiled on demand. And after compilation MySQL puts it in cache<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Limitations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Expert skill set required<\/p>\n<p>\u2022 MySQL does not provide with debugging facilities<\/p>\n<p style=\"text-align: justify\">\u2022 Memory consumption will be more if many procedures are used or many logical operations in a procedure.<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 <\/strong><\/p>\n<p><strong>\u00a0 Types of Parameters in Stored Procedure<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Procedures can also be made dynamic. The dynamism for procedures can be achieved by passing parameters before execution. The behavior of the procedure changes depending on the parameters passed.<\/p>\n<p>\u2022 The different types of parameters available with stored procedure are<\/p>\n<p>\u2022 IN : IN is the default parameter passed to the procedure which can be changed in the procedure<\/p>\n<p>\u2022 OUT : No value is passed to the procedure , but the value is returned out from the procedure.<\/p>\n<p>\u2022 IN OUT : Here , both value can be accepted and can be returned.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Structure of Stored Procedure<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Stored procedure consists of<\/p>\n<p>\u2022 Declarative part<\/p>\n<p style=\"text-align: justify\">\u2022 The declarative part contains the declarations about variables, constants, cursors, exceptions etc. The scope of these declarations is local to the procedure<\/p>\n<p>\u2022 Executable part<\/p>\n<p>\u2022 The executable part contains the actions procedure is expected to take.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-256 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-119.png\" alt=\"\" width=\"545\" height=\"377\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-119.png 545w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-119-300x208.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-119-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-119-225x156.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-119-350x242.png 350w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Now, let us understand different types of examples, with reference to stored procedures.<\/p>\n<p>&nbsp;<\/p>\n<p>All the below code blocks are taken from MySQL editor and displayed here as it is.<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Simple example to fetch data from employee table<\/p>\n<p>&nbsp;<\/p>\n<p>DELIMITER $$<\/p>\n<p>CREATE DEFINER=`root`@`localhost` PROCEDURE `get_sal`()<\/p>\n<p>BEGIN<\/p>\n<p>select name,salary from emp;<\/p>\n<p>END<\/p>\n<p>&nbsp;<\/p>\n<p>To run on command prompt SQL &gt; call get_sal();<\/p>\n<p>The above statement will display the name and salary from emp table.<\/p>\n<p>&nbsp;<\/p>\n<p>2. Example to demonstrate IN parameter<\/p>\n<p>&nbsp;<\/p>\n<p>DELIMITER $$<\/p>\n<p>CREATE DEFINER=`root`@`localhost` PROCEDURE `raise_sal`(emp_no varchar(10), amount int)<\/p>\n<p>BEGIN<\/p>\n<p style=\"text-align: justify\">UPDATE emp<\/p>\n<p style=\"text-align: justify\">SET salary = salary + amount WHERE empid = emp_no;<\/p>\n<p style=\"text-align: justify\">END<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the above example, emp_no and amount are passed as IN parameters, and the salary is updated based on the amount provided by the user. By default, parameters are IN.<\/p>\n<p>&nbsp;<\/p>\n<p>To run on command prompt SQL &gt; call raise_sal(1,100);<\/p>\n<p>&nbsp;<\/p>\n<p>The above statement will update the salary with 100 of empid : 1 , Hence the old value was 5400 which will be updated to 5500.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">3. Example to demonstrate OUT parameter<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>DELIMITER $$<\/p>\n<p style=\"text-align: justify\">CREATE DEFINER=`root`@`localhost` PROCEDURE `fetch_name`(empno varchar(10), out v_name varchar(10))research<\/p>\n<p>BEGIN<\/p>\n<p>select name into v_name from emp where empid=empno;<\/p>\n<p>END<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The above example demonstrates the use of OUT parameter and IN parameter. Empno is passed as IN parameter and name is returned as OUT parameter.<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u00a0 Example to demonstrate IN OUT parameter<\/p>\n<p>&nbsp;<\/p>\n<p>DELIMITER $$<\/p>\n<p>CREATE DEFINER=`root`@`localhost` PROCEDURE `raise_sal1`(empno varchar(10), Inout amount int)<\/p>\n<p>BEGIN<\/p>\n<p>declare v_amt int; declare v_sal int;<\/p>\n<p>select salary into v_sal from emp where empid = empno; set v_amt = v_sal + amount;<\/p>\n<p>update emp set salary = v_amt where empid = empno; set amount=v_amt;<\/p>\n<p>END<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The above example display the functionality of InOUT parameter where the variable amount is accepting the value, it is then processed and the same amount is returned back the value.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">5. Example to demonstrate creating and executing a stored procedure on SQL prompt , Example of IN OUT parameter<\/p>\n<p>&nbsp;<\/p>\n<p>Creating the procedure<\/p>\n<p>SQL&gt; create procedure square (INOUT P int) set P=P*P;<\/p>\n<p>To assign value to the variable SQL&gt; set @number=10;<\/p>\n<p>To call the procedure<\/p>\n<p>SQL&gt; call square (@number);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">To display variable value<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SQL&gt; select @number ;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">To drop a procedure<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SQL&gt; drop procedure square;<\/span><\/p>\n<\/div>\n<ul>\n<li>Procedures allow you to return multiple values, You can separate the parameters with \u201c,\u201d(comma).<\/li>\n<li>Loops can also be used into Mysql. The three types of loops which exist in MySQL are<\/li>\n<li>Syntax : While &lt;expression&gt; Do Statements<\/li>\n<li>End While<\/li>\n<li>Repeat<\/li>\n<\/ul>\n<div>\n<p>&nbsp;<\/p>\n<p>Statements<\/p>\n<p>Until &lt;expression&gt;<\/p>\n<p>End Repeat<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">MySQL also provides with Loop statement which executes code repeatedly by using Loop_label e.g. Loop _label: loop. You can use Leave and iterate statements to navigate in the loop.<\/p>\n<p>&nbsp;<\/p>\n<p>6. Example to demonstrate creating and executing a stored procedure on SQL prompt , Example of IN OUT parameter<\/p>\n<p>&nbsp;<\/p>\n<p>DELIMITER $$<\/p>\n<p>CREATE DEFINER=`root`@`localhost` PROCEDURE `test_loop`()<\/p>\n<p>BEGIN<\/p>\n<p>DECLARE cntr INT;<\/p>\n<p>DECLARE str VARCHAR(255);<\/p>\n<p>SET cntr = 1;<\/p>\n<p>SET str = &#8221;;<\/p>\n<p>loop_label: LOOP<\/p>\n<p>IF cntr &gt; 10 THEN<\/p>\n<p>LEAVE loop_label;<\/p>\n<p>END IF;<\/p>\n<p>SET cntr= cntr + 1;<\/p>\n<p>IF (cntr mod 2) THEN<\/p>\n<p>ITERATE loop_label;<\/p>\n<p>ELSE<\/p>\n<p>SET str = CONCAT(str,cntr,&#8217;,&#8217;);<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">END IF;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">END LOOP;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SELECT str;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">END<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Output: 2,4,6,8,10<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">THE Procedures which are created can be viewed by issuing the following command<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Show procedure status<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>It displays all the statistics about the procedure<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Stored Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0 Stored functions are the stored programs that return a single value. We make use of stored functions when we want to use some common routines which increases reusability.<\/p>\n<p>\u2022\u00a0 But unlike stored procedure, we can use functions with an expression.<\/p>\n<p>\u2022\u00a0 Functions can have only IN parameters<\/p>\n<p>\u2022\u00a0 The return statement is compulsory in the function with a valid SQL datatype<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-257 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-120.png\" alt=\"\" width=\"572\" height=\"409\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-120.png 572w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-120-300x215.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-120-65x46.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-120-225x161.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-120-350x250.png 350w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/p>\n<\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Simple example to fetch data from employee table<\/span><\/div>\n<div><\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0DELIMITER $$<\/span><\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0CREATE DEFINER=`root`@`localhost` FUNCTION `get_sal`(empno varchar(10)) RETURNS int(11)<\/span><\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0BEGIN<\/span><\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Declare v_sal int;<\/span><\/div>\n<div>\u00a0 \u00a0 \u00a0<span style=\"text-align: initial;font-size: 1em\">select sal into v_sal from emp where empid = empno;<\/span><\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0RETURN v_sal;<\/span><\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0END<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>To execute on command prompt<\/p>\n<p>select empid,get_sal1(1) from emp where empid=1;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The function call is used as part of expression here. The query calls the function and fetches the salary for the empid passed.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The simple example is illustrated to demonstrate the simple understanding of functions, but functions can be used to perform complex calculations and return results from complex queries.<\/p>\n<\/div>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Stored Procedures in MYSQL<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/SWWTiRw18uY\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>References:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers<\/p>\n<p style=\"text-align: justify\">2) Learning PHP, MySQL &amp; JavaScript , Robin Nixon , O\u2019Reilly Publications<\/p>\n<p style=\"text-align: justify\">3) PHP and MySQL Web Development , Luke Welling , Laura Thomson , Pearson Publications, Fourth Edition<\/p>\n<p style=\"text-align: justify\">4) MySQL Documentation : https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/create-procedure.html<\/p>\n<p style=\"text-align: justify\">5) mysqltutorial.org<\/p>\n<p>Additional Reading :<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers<\/p>\n<p style=\"text-align: justify\">2) MysQL documentation : dev.mysql.com\/doc\/ Practice Questions<\/p>\n<p style=\"text-align: justify\">1) Create a Stored Procedure Named DisplayMessage which will display the message \u201cWelcome to E-pg-Pathshala\u201d<\/p>\n<p style=\"text-align: justify\">2) Create a Procedure named AddEmp which will add records in emp table.<\/p>\n<p style=\"text-align: justify\">3) Create a Procedure DisplayMarks, which will return the total marks of a particular student. (student id will be passed as parameter)<\/p>\n<p style=\"text-align: justify\">4) Delete records from emp table based on the empid passed as the parameter. Display number of records deleted.<\/p>\n<p style=\"text-align: justify\">5) Create a Procedure Raisesal which will update the employees salary based on following conditions. (you will have to add designation field in emp table)<\/p>\n<ol>\n<li>If employee is Director : increment will be 20%<\/li>\n<li>If employee is Manager : increment will be 15%<\/li>\n<li>If employee is Programmer : increment will be 10%<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>6) Create a function to :<\/p>\n<ol>\n<li>Return the maximum salary for department 10<\/li>\n<li>Return whether the number passed is prime or not<\/li>\n<li>Return the total salary of employee after adding 10% DA and deduction 2% tax (to be done for empid passed).<\/li>\n<\/ol>\n","protected":false},"author":3,"menu_order":27,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-253","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/253\/revisions"}],"predecessor-version":[{"id":512,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/253\/revisions\/512"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/253\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=253"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=253"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}