为什么 MySQL Workbench 告诉我需要分号?

Why is MySQL Workbench telling me I need a semicolon?

此代码已通过多项在线验证测试。我不知道出了什么问题。在 CONCAT 函数之后,它说我需要一个分号,尽管那里已经有了分号。在 end 上,它表示在期望语句结束时它是无关输入。给出了什么?

create procedure AddColumnUnlessExists(
    IN dbName tinytext,
    IN tableName tinytext,
    IN fieldName tinytext,
    IN fieldDef text)
begin
    IF NOT EXISTS (
        SELECT * FROM information_schema.COLUMNS
        WHERE column_name=fieldName
        and table_name=tableName
        and table_schema=dbName
        )
    THEN
        set @ddl = CONCAT('ALTER TABLE ', dbName, '.', tableName, ' ADD COLUMN ', fieldName, ' ', fieldDef);
        prepare stmt from @ddl;
        execute stmt;
    END IF;
end;

我认为问题在于:您没有使用 DELIMITER

所以就这样说吧:

DELIMITER //
create procedure AddColumnUnlessExists(
    IN dbName tinytext,
    IN tableName tinytext,
    IN fieldName tinytext,
    IN fieldDef text)
begin
    IF NOT EXISTS (
        SELECT * FROM information_schema.COLUMNS
        WHERE column_name=fieldName
        and table_name=tableName
        and table_schema=dbName
        )
    THEN
        set @ddl = CONCAT('ALTER TABLE ', dbName, '.', tableName, ' ADD COLUMN ', fieldName, ' ', fieldDef);
        prepare stmt from @ddl;
        execute stmt;
    END IF;
end //
DELIMITER ;

编辑 https://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html

If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

To redefine the mysql delimiter, use the delimiter command. The following example shows how to do this for the dorepeat() procedure just shown. The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.

问题出在分隔符上,您应该更改分隔符。我遇到了同样的问题,更改分隔符解决了这个问题。 请参阅@Alex 答案。

这里有一个简单的解释,引用了MySQL文档:

The example uses the mysql client delimiter command to change the statement delimiter from ; to // while the procedure is being defined. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself. See Section 22.1, “Defining Stored Programs”.

https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html

DELIMITER // 
create procedure some_procedure() 
# Do what you need here

END // 

DELIMITER ; # change the delimiter back again.