19

How to Alter a stored procedure in Mysql.

DROP PROCEDURE IF EXISTS sp_Country_UPDATE; 
CREATE PROCEDURE sp_Country_UPDATE 
  ( IN p_CountryId int, 
    IN p_CountryName nvarchar(25), 
    IN p_CountryDescription nvarchar(25), 
    IN p_IsActive bit, 
    IN p_IsDeleted bit ) 
  UPDATE 
    Country 
  SET 
    CountryName = p_CountryName , 
    CountryDescription=p_CountryDescription, 
    IsActive= p_IsActive, 
    IsDeleted=p_IsDeleted 
  WHERE 
    CountryId = p_CountryId ;

How to alter this Stored Procedure?

8
  • Depends, what's your stored procedure and what have you tried? Commented Dec 20, 2012 at 10:40
  • I need the syntax to alter the stored procedure in Mysql.
    – Tejaswi
    Commented Dec 20, 2012 at 10:40
  • 1
    dev.mysql.com/doc/refman/5.0/en/alter-procedure.html there you go Commented Dec 20, 2012 at 10:41
  • DROP PROCEDURE IF EXISTS sp_Country_UPDATE; CREATE PROCEDURE sp_Country_UPDATE ( IN p_CountryId int, IN p_CountryName nvarchar(25), IN p_CountryDescription nvarchar(25), IN p_IsActive bit, IN p_IsDeleted bit ) UPDATE Country SET CountryName = p_CountryName , CountryDescription=p_CountryDescription, IsActive= p_IsActive, IsDeleted=p_IsDeleted WHERE CountryId = p_CountryId ;
    – Tejaswi
    Commented Dec 20, 2012 at 10:41
  • duplicate of [How do you edit a stored procedure in MySQL][1] [1]: stackoverflow.com/questions/2559997/…
    – czioutas
    Commented Dec 20, 2012 at 10:41

3 Answers 3

24

If you mean you want to edit the Procedure, then you can't according to the MySQL docs:

This statement can be used to change the characteristics of a stored procedure. More than one change may be specified in an ALTER PROCEDURE statement. However, you cannot change the parameters or body of a stored procedure using this statement; to make such changes, you must drop and re-create the procedure using DROP PROCEDURE and CREATE PROCEDURE.

The Alter syntax lets you change the "characteristics" but not the actual procedure itself

http://dev.mysql.com/doc/refman/5.0/en/alter-procedure.html

Here's an example of creating, Altering (the comment) then dropping and recreating:

DROP PROCEDURE myFunc;

DELIMITER //

CREATE PROCEDURE myFunc ()
COMMENT 'test'
BEGIN
SELECT 5;
END //

DELIMITER ;

ALTER PROCEDURE myFunc
COMMENT 'new comment';

CALL myFunc();

DROP PROCEDURE myFunc;

DELIMITER //

CREATE PROCEDURE myFunc ()
COMMENT 'last time'
BEGIN
SELECT 6;
END //

DELIMITER ;

CALL myFunc();

The above CALL myFunc() statments would return 5 and then 6.

Viewing the stored procedure would show a comment of "test", "new comment" or "last time" depending on when you viewed the Procedure body (I'm not sure how to view the comments via the CLI but I can see them in the functions tab in Navicat)

3
  • So we cannot modify the body, add/remove input parameter using alter. So for this we need to drop the existing stored procedure and again create it. Commented Oct 19, 2016 at 5:55
  • Not according to the docs no - unless anything has changed since I answered this in March
    – Pete
    Commented Oct 19, 2016 at 6:14
  • This is so much harder than Sql Server
    – live-love
    Commented May 19, 2021 at 16:01
1
ALTER PROCEDURE proc_name [characteristic ...]

characteristic:
    COMMENT 'string'
  | LANGUAGE SQL
  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
  | SQL SECURITY { DEFINER | INVOKER }
-9

This is how you Create

CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT *  FROM products;
END //

This is how you Alter

Alter PROCEDURE GetAllProducts()
BEGIN
SELECT *  FROM products;
END //
1
  • It is not the right way to Alter a procedure w.r.t mysql
    – sib10
    Commented Jan 10, 2018 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.