MySQL过程子查询返回null

时间:2018-09-15 01:12:43

标签: mysql null subquery procedure

我有一个mysql过程,在过程中我使用子查询来设置值,并且在子查询的途中,仅使用不同的参数,所有结构都相同,开始返回null。如果我确实将子查询复制并粘贴到sql窗口中,它将返回结果。

“服务到收入汇总”工作正常,但是在“从COGS到收入汇总”之后,“ VALUE”继续返回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 ;

0 个答案:

没有答案