函数返回值到过程mysql

时间:2016-04-06 15:25:15

标签: mysql stored-procedures handler stored-functions

我有mysql版本5.5.38。

当我调用一个调用其中函数的过程时,处理程序将其激活为函数并升级到过程。我需要函数的处理程序条件不影响程序的进程。包括功能和程序。

功能:

DELIMITER $$

CREATE FUNCTION FUNCTION_HOURLY_GAS_CHANGE(INI_DATE DATETIME, TANK INT)
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE END_GAL INT;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET END_GAL = -1;
SET END_GAL = 0;
SELECT GALLONS INTO END_GAL
    FROM TLS_TEMP_DATA WHERE FK_TANK = TANK
    AND DATE LIKE CONCAT(DATE_FORMAT(DATE_ADD(DATE_FORMAT(INI_DATE, '%Y-%m-%d %H'), INTERVAL 1 HOUR), '%Y-%m-%d %H'),':%')
    AND REQUEST_TYPE = 1;
RETURN END_GAL;
END $$
DELIMITER ;

过程:

    DELIMITER $$
    CREATE  PROCEDURE HOURLY_GAS_CHANGE(IN dateReport char(50), IN tank INT)
    BEGIN
            DECLARE COMPLETELOOP INT DEFAULT 0;
            DECLARE INI_DATE DATETIME;
            DECLARE INI_GAL INT;
            DECLARE END_GAL INT;
            DECLARE DIFF INT;
            DECLARE V_HOUR CHAR(50);
            DECLARE V_INI_GAL CHAR(50);
            DECLARE V_END_GAL CHAR(50);
            DECLARE V_DIFF CHAR(50);
            DECLARE INITIALDATA CURSOR FOR
            SELECT
                  DATE, GALLONS FROM TLS_TEMP_DATA WHERE FK_TANK = tank
                  AND (DATE BETWEEN CONCAT(dateReport, ' 00:00:00') AND CONCAT(dateReport, ' 23:59:59')) AND REQUEST_TYPE = 1
                  ORDER BY ID;
            DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET COMPLETELOOP = 1;
            DROP TEMPORARY TABLE IF EXISTS HOURLYGASCHANGE;
            CREATE TEMPORARY TABLE HOURLYGASCHANGE(
                `HOUR_DATA` CHAR(50) NOT NULL,
                `INI_GAL` CHAR(50) NOT NULL,
                `END_GAL` CHAR(50) NOT NULL,
                `DIFF` CHAR(50) NOT NULL)
                 ENGINE=MEMORY;

            OPEN INITIALDATA;
            READ_LOOP: LOOP
              FETCH INITIALDATA INTO INI_DATE, INI_GAL;
                IF COMPLETELOOP THEN
                  LEAVE READ_LOOP;
                END IF;
                SET END_GAL = 1;
                SET END_GAL = FUNCTION_HOURLY_GAS_CHANGE(INI_DATE, tank);
                IF END_GAL > 0 THEN
                 SET DIFF = (END_GAL - INI_GAL);
                 SET V_DIFF = CAST(DIFF AS CHAR(50));
         SET V_END_GAL = CAST(END_GAL AS CHAR(50));
                ELSE
                 SET V_DIFF = 'N/A';
                 SET V_END_GAL = 'NOT UPDATED';
                END IF;
                    SET V_INI_GAL = CAST(INI_GAL AS CHAR(50));
                    SET V_HOUR = CAST(DATE_FORMAT(INI_DATE, '%H') AS CHAR(50));
                    INSERT INTO HOURLYGASCHANGE VALUES(V_HOUR, V_INI_GAL, V_END_GAL, V_DIFF);
            END LOOP;
            close INITIALDATA;
            SELECT * FROM HOURLYGASCHANGE;
    END $$
    DELIMITER ;

1 个答案:

答案 0 :(得分:0)

我无法解决问题,但是我找到了一个替换函数的解决方案,我将空结果捕获到IFNULL函数中,现在我可以在select中显示一个特定值时显示空结果。

SELECT (
 IFNULL(
  (
  SELECT ENDTLS.GALLONS 
  FROM TLS_TEMP_DATA ENDTLS 
  WHERE ENDTLS.FK_TANK = tank
  AND ENDTLS.DATE LIKE ONCAT(DATE_FORMAT(DATE_ADD(DATE_FORMAT(INI_DATE, '%Y-%m-%d %H'), INTERVAL 1 HOUR), '%Y-%m-%d %H'),':%')
  AND ENDTLS.REQUEST_TYPE = 1
  )
 , -1)
) INTO END_GAL;