简体   繁体   中英

MySQL version differences when creating stored procedures?

I'm copying a development database to a web production server, which works fine until the following:

CREATE PROCEDURE sprc_list_user_accounts
    (
        int_page_number     INT,
        int_recs_per_page   INT
    )
    BEGIN
        DECLARE     int_record_first INT;
        IF          int_page_number = 0
            THEN    SELECT          record_identifier,
                                    name_account,
                                    name_last,
                                    name_first,
                                    flag_disabled
                        FROM        acs_accounts
                        ORDER BY    name_last,
                                    name_first;
            ELSE    SET int_record_first = ((int_page_number - 1) * int_recs_per_page);
                    SELECT          record_identifier,
                                    name_account,
                                    name_last,
                                    name_first,
                                    flag_disabled
                        FROM        acs_accounts
                        ORDER BY    name_last,
                                    name_first
                        LIMIT       int_record_first, int_recs_per_page;
        END IF; 
    END $$

This works flawlessly on my own machine - Win7/64 MySQL 5.5.24, but chokes on the web machine - Linux/64 MySQL 5.1.55. What's annoying is that all of the preceding tables and procedures show up just fine. The result is a not very helpful 1064 with no explanation.


The exact response was

Error

SQL query: Documentation

-- =====
-- LISTS
-- =====
CREATE PROCEDURE sprc_list_user_accounts(
int_page_number INT,
int_recs_per_page INT
) BEGIN DECLARE int_record_first INT;

SELECT COUNT( * )
FROM acs_accounts;

IF int_page_number =0 THEN SELECT record_identifier, name_account, name_last, name_first
FROM acs_accounts
WHERE flag_disabled =0
ORDER BY name_last, name_first;

ELSE SET int_record_first = ( (
int_page_number -1
) * int_recs_per_page ) ;

SELECT name_account, name_last, name_first, flag_disabled
FROM acs_accounts
ORDER BY name_last, name_first
LIMIT int_record_first, int_recs_per_page;

END IF ;

END $$

MySQL said: Documentation
#1064 - 

---END OF MESSAGE!

Using local variables in your LIMIT clause is not supported until MySQL 5.5.6.

See Bug #11918 SP does not accept variables in LIMIT clause

The workaround in earlier versions of MySQL is to use dynamic SQL. That is, you have to PREPARE and EXECUTE the query in your stored procedure.

PS: It's a good idea to develop on the same version of MySQL that you plan to deploy to.

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