Getting two issues while using stored procedure in MySQL

前端 未结 2 2089
野趣味
野趣味 2021-01-26 04:09

Below is the sample code of my Stored Procedure in which I am working on for interest calculation. This code is not executable because according to finding its getting issue whi

2条回答
  •  花落未央
    2021-01-26 04:26

    DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

    Declarations must follow a certain order. Cursor declarations must appear before handler declarations. Variable and condition declarations must appear before cursor or handler declarations.

    http://dev.mysql.com/doc/refman/5.7/en/declare.html

    That's the restriction.

    Now, the workaround: add a nested BEGIN ... END block.

    DELIMITER $$
    CREATE PROCEDURE ...
    BEGIN
      DECLARE ... INT ... -- variable
      CREATE TEMPORARY TABLE... -- following the declarations, no more declarations allowed, unless...
      BEGIN -- resets the scope, changes the rules, allows more declarations
        DECLARE ... INT ... -- variables
        DECLARE ... CURSOR ...
        DECLARE CONTINUE HANDLER ...
        OPEN ...
        ...
      END;
    END $$
    

    All the variables in the outer block are still in scope in the inner block, unless another variable in the inner block has a conflicting name.

    A HANDLER in the outer block is also in scope for signals in the inner block, unless a conflicting handler is declared there, in which case the inner handler will catch the exception and the outer handle will catch anything throw by the inner handler, including a RESIGNAL.

    Multiple nesting levels are allowed. The size of the thread_stack might be a factor, but the documentation is unclear. I've been running 262,144 byte thread stacks since before it was made the default, and have never encountered a limit.

提交回复
热议问题