How can we alter a MySQL stored procedure?

MySQLMySQLi Database

If we have ALTER ROUTINE privileges for the procedure then with the help of ALTER PROCEDURE statement we can alter a MySQL stored procedure. To demonstrate it we are taking an example of a stored procedure named ‘delete_studentinfo’ which have the following create a statement −

mysql> SHOW CREATE PROCEDURE Delete_studentinfo\G
*************************** 1. row ***************************
           Procedure: Delete_studentinfo
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_
ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `Delete_studentinfo`( IN p_id INT)
BEGIN
DELETE FROM student_info
WHERE ID=p_id;
END
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: latin1_swedish_ci
1 row in set (0.01 sec)

In the above result set, it is clear that there is no comment in the stored procedure ‘delete_studentinfo’. Now, with the help of ALTER PROCEDURE statement, we can add a comment as follows −

mysql> ALTER PROCEDURE Delete_studentinfo
   -> COMMENT 'deleting the record'//
Query OK, 0 rows affected (0.03 sec)

Now, it can be confirmed, that comments have been added in the stored procedure, with the help of result set of the following query −

mysql> SHOW CREATE PROCEDURE Delete_studentinfo\G
*************************** 1. row ***************************
           Procedure: Delete_studentinfo
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_
ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `Delete_studen
tinfo`( IN p_id INT)
   COMMENT 'deleting the record'
BEGIN
DELETE FROM student_info
WHERE ID=p_id;
END
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
raja
Published on 21-Feb-2018 14:44:39
Advertisements