You are on page 1of 5

Ex No: 8

IN and OUT of PROCEDURES

CREATE PROCEDURE Syntax

The general syntax of Creating a Stored Procedure is : CREATE PROCEDURE proc_name ([proc_parameter[......]]) routine_body proc_name : procedure name proc_parameter : [ IN | OUT | INOUT ] param_name type routine_body : Valid SQL procedure statement An IN parameter is used to pass the value into a procedure. The procedure can be change the value but when the procedure return the value then modification is not visible to the caller. An OUT parameter is used to pass the value from the procedure to the caller but its visible to the caller. An INOUT parameter is initialized by the caller and it can be modified by the procedure, and any change made by the procedure is visible to the caller. By default each parameter is an IN parameter.
ALTER PROCEDURE Alter Procedure statement is used to change access permissions that preserves by the procedure. And ALTER PROCEDURE needs the use of the same encryption and recompile option as the original CREATE PROCEDURE command.

mysql> ALTER PROCEDURE Sproc SQL SECURITY DEFINER; Query OK, 0 rows affected (2.00 sec)

With the ALTER PROCEDURE Statement you can change only the characteristics and if you want to change in statement list then you have to DROP the procedure and CREATE again. DROP PROCEDURE Statement is used to drop a Procedure or Function. But for dropping them you must have the ALTER ROUTINE privilege. If IF NOT EXISTS clause is available then its prevents you from occurring an error when the procedure or function does not exist its produced only a warning.

mysql> DROP PROCEDURE IF EXISTS Sproc; Query OK, 0 rows affected (0.00 sec)
CALL Statement Syntax
The CALL statement is used to call a procedure, which has been defined previously. CALL can return the values to its caller through its parameters that are declared as OUT or INOUT parameters. This statement is also used to returns the number of rows affected that a client program can obtain at the SQL level by calling the ROW_COUNT(). The general syntax of CALL Statement is : CALL p_name([parameter[,...]]) The following example shows you the use of CALL statement. Example :

mysql> delimiter // mysql> CREATE PROCEDURE Sp1(OUT p VARCHAR(20),OUT p1 VARCHAR(20),IN p2 INT) -> SELECT Ename,City INTO p,p1 FROM Emp WHERE Eid=p2; -> // Query OK, 0 rows affected (0.02 sec) mysql> delimiter ;

mysql> CALL Sp1(@Name,@City,1); Query OK, 0 rows affected (0.00 sec) mysql> SELECT @Name,@City;
+-------+-------+ | @Name | @City | +-------+-------+ | Rahul | Delhi | +-------+-------+ 1 row in set (0.00 sec)

mysql> SELECT * FROM Emp;


+-----+---------+----------+-------------------+--------+-------+ | Eid | Ename | City | Designation | Salary | Perks | +-----+---------+----------+-------------------+--------+-------+ | 1 | Rahul | Delhi | Manager | 10300 | 853 | | 2 | Gaurav | Mumbai | Assistant Manager | 10300 | 853 | | 3 | Chandan | Banglore | Team Leader | 15450 | 999 | | 5 | Tapan | Pune | Developer | 20600 | 1111 | | 6 | Amar | Chennai | Developer | 16000 | 1124 | | 7 | Santosh | Delhi | Designer | 10000 | 865 | +-----+---------+----------+-------------------+--------+-------+ 6 rows in set (0.00 sec) mysql> delimiter //

mysql> CREATE PROCEDURE Proc(OUT p VARCHAR(20), OUT p1 VARCHAR(20),IN p2 INT) -> BEGIN -> INSERT INTO Emp VALUES(p2,'Suman','Pune','Web Designer',20000,965); -> SELECT Ename,City INTO p,p1 FROM Emp WHERE Eid=p2; -> END -> //
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;

mysql> CALL Proc(@Name,@City,8); Query OK, 0 rows affected (0.03 sec)


mysql> SELECT @Name,@City;
+-------+-------+ | @Name | @City | +-------+-------+ | Suman | Pune | +-------+-------+ 1 row in set (0.00 sec)

mysql> SELECT * FROM Emp;


+-----+---------+----------+-------------------+--------+-------+ | Eid | Ename | City | Designation | Salary | Perks | +-----+---------+----------+-------------------+--------+-------+ | 1 | Rahul | Delhi | Manager | 10300 | 853 | | 2 | Gaurav | Mumbai | Assistant Manager | 10300 | 853 | | 3 | Chandan | Banglore | Team Leader | 15450 | 999 | | 5 | Tapan | Pune | Developer | 20600 | 1111 | | 6 | Amar | Chennai | Developer | 16000 | 1124 | | 7 | Santosh | Delhi | Designer | 10000 | 865 | | 8 | Suman | Pune | Web Designer | 20000 | 965 | +-----+---------+----------+-------------------+--------+-------+ 7 rows in set (0.00 sec)

mysql>

delimiter //

mysql> CREATE PROCEDURE Sproced(OUT p VARCHAR(20),OUT p1 VARCHAR(20),IN p2 INT) -> BEGIN -> DECLARE a VARCHAR(20); -> DECLARE b INT; -> SET b=p2; -> SET a='Dev%'; -> SELECT * FROM Emp WHERE Designation LIKE a; -> SELECT Ename,City INTO p,p1 FROM Emp WHERE Eid=b; -> END -> //
Query OK, 0 rows affected (0.07 sec)

mysql> delimiter ;

mysql> CALL Sproced(@Name,@City,5);


+-----+-------+---------+-------------+--------+-------+ | Eid | Ename | City | Designation | Salary | Perks | +-----+-------+---------+-------------+--------+-------+ | 5 | Tapan | Pune | Developer | 20600 | 1111 | | 6 | Amar | Chennai | Developer | 16000 | 1124 | +-----+-------+---------+-------------+--------+-------+ 2 rows in set (0.05 sec) Query OK, 0 rows affected (0.10 sec) mysql> SELECT @Name,@City; +-------+-------+ | @Name | @City | +-------+-------+ | Tapan | Pune | +-------+-------+ 1 row in set (0.01 sec)

You might also like