MySQL 建立儲存過程報錯

feelpurple發表於2016-09-08
mysql> delimiter $$
mysql> CREATE FUNCTION check_rollbackPatch(THE_REFERNCE_LABEL VARCHAR(75), THE_COLUMN_NAME VARCHAR(75), THE_INSTALL_VERSION VARCHAR(75), THE_VERSION_LEVEL_TABLE_NAME VARCHAR(75))
    ->     RETURNS INTEGER DETERMINISTIC
    ->     BEGIN
    ->         DECLARE INSTALL_VERSION VARCHAR(80) DEFAULT NULL;
    ->         DECLARE REFERENCE_LABEL VARCHAR(80) DEFAULT NULL;
    ->         DECLARE THE_QUERY VARCHAR(500) DEFAULT NULL;
    ->         DECLARE too_many_rows CONDITION FOR 1172;
    ->         DECLARE CONTINUE HANDLER FOR too_many_rows
    ->         BEGIN
    ->         SELECT concat('[INFO]    Several ', THE_REFERNCE_LABEL, ' references found.');
    ->         RETURN 0;
    ->         END;
    ->         DECLARE CONTINUE HANDLER FOR NOT FOUND
    ->         BEGIN
    ->         /*SELECT concat('[INFO]    ', THE_REFERNCE_LABEL, ' reference not found.');*/
    ->         RETURN 1;
    ->         END;        
    ->         SELECT concat('', THE_COLUMN_NAME, ', C_INSTALL_VERSION') INTO @V_PART1;
    ->         SELECT concat(THE_COLUMN_NAME, '=''', THE_REFERNCE_LABEL, ''' and C_INSTALL_VERSION=''', THE_INSTALL_VERSION, '''') INTO @V_PART2;
    ->         SELECT format_selectQuery(THE_VERSION_LEVEL_TABLE_NAME, @V_PART1, @V_PART2) INTO THE_QUERY;
    ->         /*SELECT concat('[INFO]    The query to execute is [', THE_QUERY, ']');*/
    ->         /*EXECUTE IMMEDIATE THE_QUERY into REFERENCE_LABEL, INSTALL_VERSION;*/
    ->         /*SELECT concat('[INFO]    ', THE_REFERNCE_LABEL, ' reference found');*/
    ->         RETURN 0;
    ->     END $$
ERROR 1415 (0A000): Not allowed to return a result set from a function
mysql> delimiter ;

報錯原因:
在MySQL的function裡,不能使用SELECT語句來返回結果集,會報錯。

註釋掉後,報錯消失
mysql> delimiter $$
mysql> CREATE FUNCTION check_rollbackPatch(THE_REFERNCE_LABEL VARCHAR(75), THE_COLUMN_NAME VARCHAR(75), THE_INSTALL_VERSION VARCHAR(75), THE_VERSION_LEVEL_TABLE_NAME VARCHAR(75))
    ->     RETURNS INTEGER DETERMINISTIC
    ->     BEGIN
    ->         DECLARE INSTALL_VERSION VARCHAR(80) DEFAULT NULL;
    ->         DECLARE REFERENCE_LABEL VARCHAR(80) DEFAULT NULL;
    ->         DECLARE THE_QUERY VARCHAR(500) DEFAULT NULL;
    ->         DECLARE too_many_rows CONDITION FOR 1172;
    ->         DECLARE CONTINUE HANDLER FOR too_many_rows
    ->         BEGIN
    ->         /*SELECT concat('[INFO]    Several ', THE_REFERNCE_LABEL, ' references found.');*/
    ->         RETURN 0;
    ->         END;
    ->         DECLARE CONTINUE HANDLER FOR NOT FOUND
    ->         BEGIN
    ->         /*SELECT concat('[INFO]    ', THE_REFERNCE_LABEL, ' reference not found.');*/
    ->         RETURN 1;
    ->         END;        
    ->         SELECT concat('', THE_COLUMN_NAME, ', C_INSTALL_VERSION') INTO @V_PART1;
    ->         SELECT concat(THE_COLUMN_NAME, '=''', THE_REFERNCE_LABEL, ''' and C_INSTALL_VERSION=''', THE_INSTALL_VERSION, '''') INTO @V_PART2;
    ->         SELECT format_selectQuery(THE_VERSION_LEVEL_TABLE_NAME, @V_PART1, @V_PART2) INTO THE_QUERY;
    ->         /*SELECT concat('[INFO]    The query to execute is [', THE_QUERY, ']');*/
    ->         /*EXECUTE IMMEDIATE THE_QUERY into REFERENCE_LABEL, INSTALL_VERSION;*/
    ->         /*SELECT concat('[INFO]    ', THE_REFERNCE_LABEL, ' reference found');*/
    ->         RETURN 0;
    ->     END $$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2124732/,如需轉載,請註明出處,否則將追究法律責任。

相關文章