简体   繁体   English

MySQL存储过程检查表是否存在,然后更改语法错误

[英]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. 我在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). 如果在DROP PROCEDURE之后和END之后添加Delimiter //,则在Delimiter末尾会出现错误(在此位置delimiter是无效输入)。 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 上面的更新是我的新更改,它在末尾报告错误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. 这个答案来自@Solarflare,因为他/她没有发布答案,所以我去做,这样我就可以将此问题标记为已回答,这可能对其他可能遇到与我同样问题的新手很方便。

You have to leave a space between delimiter and ; 您必须在定界符和之间留一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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