Suraz Suraz - 2 months ago 8
SQL Question

Getting two issues while using stored procedure in MySQL

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 while defining creating temporary table block before the cursor declaration but if I define same thing recently after cursor declaration then it's executing successfully.

1- My question is I am using that table inside cursor so I must have to define after cursor or I have missed anything ??

CREATE PROCEDURE `sp_interest_calculation_test`(
IN sub_type CHAR(1)
)
BEGIN
DECLARE s_ledger_id INT;
DECLARE s_start, s_end, s_tran INT DEFAULT 0;

**DROP TABLE IF EXISTS tmp_interest;
CREATE TEMPORARY TABLE IF NOT EXISTS tmp_interest(
id int(11) NOT NULL AUTO_INCREMENT,
ledger_id INT UNSIGNED,
dr_amount INT,
cr_amount INT,
balance INT
);**

DECLARE cur_saving_acc CURSOR FOR
SELECT SQL_CALC_FOUND_ROWS 1;

OPEN cur_saving_acc;

SET s_end = (SELECT FOUND_ROWS());

WHILE s_start<s_end DO
FETCH cur_saving_acc INTO s_ledger_id;

INSERT INTO tmp_interest(ledger_id)
SELECT s_ledger_id;

SELECT * FROM tmp_interest;

/*Interest calculation logic ends here */
SET s_start = s_start+1;

END WHILE;

CLOSE cur_saving_acc;

END


2- After successful execution of above stored procedure (defining temporary table after cursor declaration), I got the following issue when I call the SP:

CALL sp_interest_calculation_test ('A');


"Error Code : 1075
Incorrect table definition; there can be only one auto column and it must be defined as a key"

It is because I missed to define id as primary key so I simply changed column with

id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY


I am using stored procedure in MySQL first time and it was hectic for me to fix the above issue so can you please describe me the above issues and their reasons and it there anyway to define auto number column without defining that as primary key ?

Note: Parameter sub_type is not used anywhere and there isn't more operations performed within cursor in the sample code though I am dealing with heavy interest calculation inside the block.

Thank you very much in advance and hope all you will pay some interest in this question. (I didn't find anything exact for these issues). I am also looking for alternatives of it, simply I have to calculate debit,credit,balance for each ledger, I tried it by without loop in single query but execution never ends.

Answer

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.

Comments