Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop procedure if exists in mysql

Hi i am trying to create a mysql script that I can run whenever I need to update my database. The script creates a table and then executes some stored procedures.

DELIMITER $$

CREATE TABLE IF NOT EXISTS tbl_name (
    col1 bigint(20) NOT NULL AUTO_INCREMENT,
    col2  varchar(255) NOT NULL,
    col3 varchar(64) NOT NULL,
    col4 datetime DEFAULT NULL,
    PRIMARY KEY (`col1 `),
    UNIQUE KEY col2  (`col2`)
) ENGINE=InnoDB AUTO_INCREMENT=572 DEFAULT CHARSET=utf8$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN username 
VARCHAR(255))
BEGIN
    DECLARE var1 VARCHAR(64);
    DECLARE expirationDate DATETIME;

    SET var1 = 12345;
    SET expirationDate = DATE_ADD(NOW(), INTERVAL 30 SECOND);

    REPLACE INTO tbl_name (col2, col3, col4) VALUES (someval, var1, expirationDate);
END$$

DELIMITER ;

When I ran the script first time, it created the table and executed the stored procedure in MySQL Workbench. When I ran the same thing second time, I got the error 1304 procedure already exists.

I looked online here about dropping the procedure and then create again. But when I entered the below command before creating the procedure, i got an error on CREATE command with code 1064.

DROP PROCEDURE IF EXISTS myproc;
CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN username 
    VARCHAR(255))
.
.
.

I am very new to mysql and not sure how to execute the procedure if it already exists.

Any help would be appreciated.

like image 897
Naphstor Avatar asked Oct 20 '17 19:10

Naphstor


People also ask

How do I drop a procedure if exists?

DROP PROCEDURE removes the definition of one or more existing procedures. To execute this command the user must be the owner of the procedure(s). The argument types to the procedure(s) usually must be specified, since several different procedures can exist with the same name and different argument lists.

How do I delete an existing procedure in MySQL?

The syntax to a drop a procedure in MySQL is: DROP procedure [ IF EXISTS ] procedure_name; procedure_name. The name of the procedure that you wish to drop.

How do I drop an existing procedure in SQL?

Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to remove, and then click Delete.

How do you drop a stored procedure if it exists in SQL Server?

To drop the procedure, we have to write a conditional statement to check if the store procedure exists or not then write the drop statement. Otherwise, it will raise an error in case the stored procedure does not exist.


1 Answers

Since you changed to DELIMITER $$ you need to use that delimiter at the end of each statement until you change it back.

DROP PROCEDURE and CREATE PROCEDURE are separate statements, and each requires its own statement delimiter.

DROP PROCEDURE IF EXISTS myproc $$

Note the delimiter at the end of the line above.

CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN username 
    VARCHAR(255))
.
.
.
END $$

And another delimiter at the end of the whole CREATE PROCEDURE statement.

like image 102
Bill Karwin Avatar answered Oct 01 '22 18:10

Bill Karwin