MySQL - ALTER PROCEDURE Statement



MySQL ALTER PROCEDURE Statement

Stored procedures are sub routines, segment of SQL statements which are stored in SQL catalog. All the applications that can access Relational databases (Java, Python, PHP etc.), can access stored procedures.

Stored procedures contain IN and OUT parameters or both. They may return result sets in case you use SELECT statements. Stored procedures can return multiple result sets.

You can alter the characteristics of a procedure using the ALTER PROCEDURE statement.

Syntax

Following is the syntax of the ALTER PROCEDURE statement −

ALTER PROCEDURE procedure_name COMMENT 'string', LANGUAGE SQL;

Where, procedure_name is the name of the procedure you need to alter.

Example

Suppose we have created a table named Employee in the database using the following CREATE statement −

CREATE TABLE Employee(Name VARCHAR(255),Salary INT,Location VARCHAR(255));

Now, let us insert some records into the Employee table using the following query −

INSERT INTO Employee VALUES 
('Amit', 6554, 'Hyderabad'),
('Sumith', 5981, 'Vishakhapatnam'),
('Sudha', 7887, 'Vijayawada');

Let us create a stored procedure named myProcedure. Here we are inserting a column in to the Employee table taking values from user. (through input parameters)

DELIMITER //
Create procedure myProcedure (
   IN name VARCHAR(30),
   IN sal INT,
   IN loc VARCHAR(45))
      BEGIN
      INSERT INTO Employee(Name, Salary, Location) VALUES (name, sal, loc);
      END //
DELIMITER ;

Following query alters the procedure myProcedure, it changes/adds comment to the above created function −

ALTER PROCEDURE myProcedure COMMENT 'This is a sample comment';

Verification

You can verify the altered procedure using the Show Create Procedure statement as shown below −

SHOW CREATE PROCEDURE myProcedure\G;

Following is the output of the above query −

************* 1. row *************
      Procedure: myProcedure
      sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
   Create Procedure: CREATE DEFINER=`root`@`localhost` 
                     PROCEDURE `myProcedure`(
   IN name VARCHAR(30),
   IN sal INT,
   IN loc VARCHAR(45))
   COMMENT 'This is a sample comment'
BEGIN
      INSERT INTO Employee(Name, Salary, Location) VALUES (name, sal, loc);
END
character_set_client: cp850
collation_connection: cp850_general_ci
   Database Collation: utf8mb4_0900_ai_ci
1 row in set (0.07 sec)
Advertisements