AAron
AAron

Reputation: 55

MySQL Syntax Error While Creating Stored Procedure

I'm trying to write a stored procedure for the first time in MySQL on MySQL Workbench. For the life of me I can't seem to get the syntax to work... this is what I have:

CREATE PROCEDURE Add_Column()
BEGIN
IF NOT EXISTS (SELECT NULL FROM information_schema.COLUMNS WHERE table_name = `Users` AND table_schema = DATABASE() AND column_name = `CreatedTimestamp`)
    THEN ALTER TABLE `Users` ADD `CreatedTimestamp` timestamp NULL DEFAULT NULL
END

CALL Add_Column();

This is the error I'm getting: Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 5

I'm running MySQL 8.0.19. Any help is appreciated!

Upvotes: 0

Views: 138

Answers (1)

Amit Verma
Amit Verma

Reputation: 2490

As per the syntax mention by MySQL if have modified. Try this

DELIMITER //
CREATE PROCEDURE Add_Column()
BEGIN
IF NOT EXISTS (SELECT NULL FROM information_schema.COLUMNS WHERE table_name = `Users` AND table_schema = DATABASE() AND column_name = `CreatedTimestamp`)
    THEN 
       ALTER TABLE `Users` ADD `CreatedTimestamp` timestamp NULL DEFAULT NULL ;
END IF;
END //
DELIMITER ;


CALL Add_Column();

Upvotes: 1

Related Questions