0

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!

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.