简体   繁体   中英

MySQL Stored Procedure check if table exists then Alter Syntax Error

I have stored procedure as below:

DROP PROCEDURE IF EXISTS TEST;
CREATE PROCEDURE TEST()
BEGIN
    IF (SELECT count(*) FROM (SELECT table_name 
                FROM INFORMATION_SCHEMA.TABLES
               WHERE table_name LIKE 'User') A) > 0 THEN
        ALTER TABLE User DROP FOREIGN KEY FK_forkey
    END IF;
END

SELECT ....

I have the syntax error at END IF. I have been trying many ways to fix this but no success so far. If I add Delimiter // after the DROP PROCEDURE and after the END, I get error at the end Delimiter (delimiter is not valid input at this position). What did I miss/How did I do it wrong? Thank you in advance for your response.

UPDATED

DROP PROCEDURE IF EXISTS TEST;
DELIMITER //
CREATE PROCEDURE TEST()
BEGIN
    IF EXISTS(SELECT table_name 
                FROM INFORMATION_SCHEMA.TABLES
               WHERE table_name LIKE 'User')
    THEN
       ALTER TABLE User DROP FOREIGN KEY FK_AccountID;
    END IF;
END //
DELIMITER; 

The updated above is my new change and it reports error at the end Delimiter: Delimiter is not valid input at this position

This answer is from @Solarflare, since he/she didn't post the answer, I'll do it instead so I could mark this question as answered and it may come in handy for others newbies that might run into the same problem as me.

You have to leave a space between delimiter and ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM