存储过程中无法在CASE内部更改表

时间:2019-01-03 09:44:50

标签: mysql stored-procedures

我正在尝试在MySQL上做一个相当简单的存储过程,但是它会不断抛出错误。

还有其他WHEN条条件,但是为了简化起见,我在这里删除了它们,并且此更简单的版本也不起作用。我得到SQL Error 1064 in Statement #2: You have an error in your SQL Syntax... near ELSE CALL finance.sprProce...

finance.spProcessTrans_AddToLogs很好,我在许多其他SP中使用了确切的语法。在WHEN之间处理的代码很好,因为我已经对其进行了单独测试。

版本:Windows / MySQL 8.0.12社区

我要去哪里错了?我应该能够在一个案例中执行此ALTER TABLE吗?

DELIMITER //
DROP PROCEDURE IF EXISTS finance.spProcessIndex;

CREATE PROCEDURE finance.spProcessIndex(
    IN tblName VARCHAR(50),
    IN actDesc VARCHAR(50)
    )
    BEGIN

    SET tblName = IFNULL(tblName, 'ERROR');     
    SET actDesc = IFNULL(actDesc, 'ERROR');     

    CASE
    WHEN actDesc='CREATE' THEN
        CASE
        WHEN tblName='tbl_transactions' THEN
            ALTER TABLE tbl_transactions 
                MODIFY TransactionID INT AUTO_INCREMENT PRIMARY KEY,
                ADD INDEX IDX_ProcessTrans_A (CustomerRef, TransMonth, Product, TransValue, RowReference, TransactionID,ProdInCust_Mnth_Same_SameProd_LowerVal),
                ADD INDEX IDX_tbl_transactions_product (Product(25)),
                ADD INDEX IDX_tbl_transactions_prodval (Product, TransValue);
        ELSE
            ALTER TABLE tbl_transactions_tmp_worker_aa 
                MODIFY TransactionID INT AUTO_INCREMENT PRIMARY KEY,
                ADD INDEX IDX_ProcessTrans_A (CustomerRef, TransMonth, Product, TransValue, RowReference, TransactionID,ProdInCust_Mnth_Same_SameProd_LowerVal),
                ADD INDEX IDX_tbl_transactions_product (Product(25)),
                ADD INDEX IDX_tbl_transactions_prodval (Product, TransValue);
        END;
    ELSE
        CALL finance.spProcessTrans_AddToLogs('spProcessIndex','BREAKPOINT','ERROR: Bad ACTION',CONCAT('A bad ACTION was specified {',actDesc,'}'));
    END;

    END//
DELIMITER ;

1 个答案:

答案 0 :(得分:1)

尝试:

DELIMITER //

-- DROP PROCEDURE IF EXISTS finance.spProcessIndex;
DROP PROCEDURE IF EXISTS finance.spProcessIndex//
.
.
.
CASE
  WHEN actDesc='CREATE' THEN
.
.
.
  CASE
    WHEN tblName='tbl_transactions' THEN
  .
  .
  .
  END CASE;
END CASE;
.
.
.

DELIMITER ;