MySQL procedure subquery returns null

Deadly 提交于 2021-01-29 10:16:52

问题


I have a mysql procedure where I'm setting values using subqueries as it progresses, and partway through the subqueries, all structured the same only using different parameters, start returning null. If I literally copy and paste the subquery into a sql window, it returns a result.

Service Revenue to Income Summary works just fine, but after "COGS to Income Summary", "VALUE" continues to return NULL

DELIMITER $$
CREATE DEFINER=`shopf740`@`localhost` PROCEDURE `PeriodEnd`(IN `processdate` DATE)
    MODIFIES SQL DATA
BEGIN

    DECLARE GLID INT;
    DECLARE VALUE DECIMAL(11,2);
    DECLARE DESCRIPTION VARCHAR(150);
    DECLARE finished integer default 0;

    DECLARE MYCURS CURSOR FOR
    SELECT
        SUM(A.DEBIT) - SUM(A.CREDIT) VALUE,
        A.GLID,
        B.DESCRIPTION
    FROM
        GL_DETAIL A
    JOIN
        GL_HEADER B
    ON
        A.GLID = B.ACCOUNT
    WHERE
        B.CLASS IN ('EXPENSE')
    GROUP BY
        A.GLID,
        B.TYPE,
        B.DESCRIPTION
    HAVING
        SUM(A.DEBIT) - SUM(A.CREDIT) != 0;

    DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET finished = 1;  


    #* Service Revenue to Income Summary*
    SET VALUE = COALESCE((SELECT SUM(CREDIT) - SUM(DEBIT) AS `VALUE` FROM GL_DETAIL WHERE GLID = 301),0);
    IF 
        VALUE != 0
    THEN
        CALL JournalEntry(processdate,301,303,VALUE,CONCAT('Closing Service Revenue To Income Summary for period ending ',processdate),'CloseServRev');
    END IF;
    # * COGS to Income Summary *
    SET VALUE = COALESCE((SELECT SUM(DEBIT) - SUM(CREDIT) AS `VALUE` FROM GL_DETAIL WHERE GLID = 302),0);

    CALL JournalEntry(processdate,303,302,VALUE,CONCAT('Closing COGS With Income Summary for period ending ',processdate),'CloseCogs');


    #*Close Owner Drawings to Owner Capital*
    SET VALUE = COALESCE((SELECT SUM(DEBIT) - SUM(CREDIT) AS `VALUE` FROM GL_DETAIL WHERE GLID = 902),0);

    CALL JournalEntry(processdate,901,902,VALUE,CONCAT('Closing COGS With Income Summary for period ending ',processdate),'CloseOwnerDrawings');


    #*Close Income Summary to Owner Capital *
    SET VALUE = COALESCE((SELECT SUM(CREDIT) - SUM(DEBIT) AS `VALUE` FROM GL_DETAIL WHERE GLID = 303),0);

    CALL JournalEntry(processdate,303,901,VALUE,CONCAT('Closing Income Summary to Owner Capital for period ending ',processdate),'CloseIncomeToCapital');

    # *Close Expense accounts to Income Summary*
    OPEN MYCURS;

    my_loop: LOOP
        FETCH NEXT FROM MYCURS INTO
            VALUE,
            GLID,
            DESCRIPTION
            ;

        IF 
            finished = 1 THEN LEAVE my_loop;
        ELSE

            CALL JournalEntry(processdate,303,GLID,VALUE,CONCAT('Closing ',DESCRIPTION,' To Income Summary for period ending ',processdate),concat('CloseExpense',GLID));

        END IF;
    END LOOP;


END$$
DELIMITER ;

来源:https://stackoverflow.com/questions/52340791/mysql-procedure-subquery-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!