mysql存储过程中的游标无法正常工作

时间:2013-06-10 08:31:01

标签: mysql

我正在使用以下存储过程并在其中调用游标但它返回零数据获取或处理过程中的错误?

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_ProcessData`(
DummyDate datetime
)
BEGIN
DECLARE MONTH BIGINT;
DECLARE YEAR BIGINT;
declare LBrCode VARCHAR(100);
declare EntryDate datetime;
declare VcrAcctId char(32);
declare DrCr VARCHAR(100);
declare FcyTrnAmt VARCHAR(100);
DECLARE CustName char(50);
DECLARE CreditAmount decimal(18,2);
DECLARE DebitAmount decimal(18,2);
DECLARE BatchCd char(15);
DECLARE SetNo BIGINT;
DECLARE ScrollNo BIGINT;
DECLARE BookType char(10);
DECLARE ActualAcctId varchar(50);
DECLARE ActualBrCode varchar(50);
DECLARE ActualBookType varchar(50);
declare curProcessAMLData cursor

    for

    select distinct * from DummyTable 
    WHERE EntryDate = DummyDate
    AND (ActualBookType = 'XX');
    open curProcessAMLData;
    fetch next from curProcessAMLData into LBrCode,EntryDate,VcrAcctId,DrCr,FcyTrnAmt,BatchCd,SetNo,ScrollNo,BookType,ActualAcctId,ActualBrCode,ActualBookType;
    while fetch_status = 0
    DO  
                SET MONTH = MONTH(DummyDate);
                SET YEAR = MONTH(DummyDate);
                IF(DrCr = 'C')
                THEN
                    SET CreditAmount = FcyTrnAmt;
                    IF NOT EXISTS (SELECT * FROM Master WHERE BranchCode = ActualBrCode AND AccNo = ActualAcctId AND TransMonth = MONTH and TransYear = YEAR)
                    THEN

                        INSERT INTO Master
                        (
                            TransDate,
                            BranchCode,
                            AccNo,
                            Credit,
                            TransMonth,
                            TransYear
                        )
                        SELECT

                                  EntryDate,
                                  ActualBrCode,
                                  ActualAcctId,
                                  CreditAmount,
                                  MONTH,
                                  YEAR
                    END;
                    ELSE
                        UPDATE Master
                            SET Credit = IFNULL(Credit,0) + CreditAmount
                            WHERE BranchCode = ActualBrCode AND AccNo = ActualAcctId 
                            AND MONTH(TransDate) = MONTH 
                                AND YEAR(TransDate) = YEAR;
                    END IF;
                ELSE
                    SET DebitAmount = FcyTrnAmt;
                    IF NOT EXISTS (SELECT * FROM Master WHERE BranchCode = ActualBrCode AND AccNo = ActualAcctId AND TransMonth = MONTH and TransYear = YEAR)
                    THEN

                        INSERT INTO Master
                        (
                            TransDate,
                            BranchCode,
                            AccNo,
                            Debit,
                            TransMonth,
                            TransYear
                        )
                        SELECT

                                  EntryDate,
                                  ActualBrCode,
                                  ActualAcctId,
                                  DebitAmount,
                                  MONTH,
                                  YEAR
                    END;
                    ELSE
                            UPDATE Master
                            SET Debit = IFNULL(Debit,0) + DebitAmount
                            WHERE BranchCode = @ActualBrCode AND AccNo = ActualAcctId 
                            AND MONTH(TransDate) = MONTH 
                                AND YEAR(TransDate) = YEAR;

                END IF;
                END IF;
        SET FcyTrnAmt = 0 ;
    END WHILE;  
fetch next from curProcessAMLData
 into LBrCode,EntryDate,VcrAcctId,DrCr,FcyTrnAmt,BatchCd,SetNo,ScrollNo,BookType,ActualAcctId,ActualBrCode,ActualBookType;
CLOSE curProcessAMLData;
END

我必须做些什么改变才能让它发挥作用?

0 个答案:

没有答案