A CREATE script exported by MySQL Workbench has sy

2019-09-01 14:21发布

问题:

I created a database on my computer, with MySQL 5.5 and MySQL Workbench 5.2.34 CE installed. Then i want to migrate the database to another computer, which has MySQL 5.0 installed. (I just need to migrate the schema, data are not needed)

I use the MySQL Workbench's File -> Export -> Forward Engineer SQL CREATE script to generate db.sql script and copy it to the other computer.

I type mysql < db.sql to create the database but only to receive an error.

Error occurs here:

DELIMITER $$

CREATE PROCEDURE `filedb`.`GetIncompleteFileId` (in latestFileId BIGINT UNSIGNED, in serverBits BIT(32), in fileCount SMALLINT UNSIGNED)

BEGIN
    SELECT `id`, `key`, `length`, `path`
    FROM
    (
        SELECT * FROM `filedb`.`fileInfo` WHERE `id` <= latestFileId
    ) AS TempTable
    WHERE (serverBits & `serverownership` NOT IN (serverBits, 0))
    ORDER BY `id` DESC
    LIMIT fileCount;
END

$$

The error is near 'fileCount; END'. If i remove the "LIMIT fileCount", the error disappears.

Anyone tells me what is the problem?

回答1:

In the MySQL 5.0 LIMIT arguments must be nonnegative integer constants, not a variables or procedure parameters. Use prepared statements to avoid this error.

From the MySQL 5.0 documentation -

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).

From the MySQL 5.5 documentation -

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:

Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.

Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.