18
I have some procedures Mysql to do the following:
The main code will always call the trial
CALL sp_syncTabela
.To
sp_syncTabela
will check if there is another specific process for thetableName
passed as argument. If it exists, it will be called; if not, the "default"sp_syncExecuteQuery
will be called in place.For example, I have the
sp_syncTabela_caixa
in the database. So when I runCALL sp_syncTabela (..."caixa"...)
, she will internally callCALL sp_syncTabela_caixa
. But a call toCALL sp_syncTabela (..."outra"...)
will only use the standard designsp_syncExecuteQuery
.All specific procedures have the format
sp_syncTabela_???
, as the "box" has. These procedures serve to perform other custom tasks besides callingCALL sp_syncExecuteQuery
.The logic is: The table referenced by the argument
tableName
has a specific process? If you have, do some custom tasks to the table and then callsp_syncExecuteQuery
; but if not, just runsp_syncExecuteQuery
with no extra task.
I don’t see any recursive calls, but I’m getting the error
Error Code: 1444. The Prepared statement contains a stored routine call that refers to that same statement. It’s not allowed to execute a Prepared statement in such a recursive Manner.
This is happening after running CALL sp_syncTabela ("I", 33, "caixa", "id='20',fundo='11.11',abertura_user_id='2',abertura_data='2014-11-11 18:24:33',fechamento_user_id=NULL,fechamento_data=NULL,", "id=20")
.
Now, the codes:
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_syncTabela`(IN action VARCHAR(10), IN tableName VARCHAR(50), IN columnData VARCHAR(4000))
BEGIN
DECLARE res Int;
SELECT Count(*) INTO res FROM information_schema.Routines WHERE routine_name = Concat("sp_syncTabela_", tableName);
CASE res
WHEN 0 THEN CALL sp_syncExecuteQuery (action, tableName, columnData);
ELSE
BEGIN
SET @querySync = Concat("CALL sp_syncTabela_", tableName, " (\"", action, "\", \"", tableName, "\", \"", columnData, "\");");
PREPARE tablePreviewQuery FROM @querySync;
EXECUTE tablePreviewQuery;
END;
END CASE;
END
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_syncTabela_caixa`(IN action VARCHAR(10), IN tableName VARCHAR(50), IN columnData VARCHAR(4000))
BEGIN
/* Apenas uma procedure específica de exemplo, com uma tarefa adicional antes de chamar sp_syncExecuteQuery */
INSERT INTO configuration (name, value) VALUES ("Última chamada à sp_syncTabela_caixa", Cast(Now() as Char))
ON DUPLICATE KEY UPDATE value = Cast(Now() as Char);
CALL sp_syncExecuteQuery (action, tableName, columnData);
END
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_syncExecuteQuery`(IN action VARCHAR(10), IN tableName VARCHAR(50), IN columnData VARCHAR(4000))
BEGIN
CASE action
WHEN "I" then CALL sp_generateInsertQuery (tableName, columnData, @queryToExecute);
WHEN "U" then CALL sp_generateUpdateQuery (tableName, columnData, @queryToExecute);
WHEN "D" then CALL sp_generateDeleteQuery (tableName, columnData, @queryToExecute);
ELSE Begin End;
END CASE;
PREPARE tablePreviewQuery FROM @queryToExecute;
EXECUTE tablePreviewQuery;
END
I have looked for help on several websites, in Portuguese and English, for keywords like 'nested procedures', 'recursive errors', and similar. And, after all, I don’t see where there is recursion in this process. When I run directly to sp_syncTabela_caixa
, no error occurs.
Ulysses, you declared that sp_syncTable will receive 3 parameters, and then made the call from sp passing 5 parameters CALL sp_syncTable ("I", 33, "box", "id='20',background='11.11',open_user_id='2',open_data='2014-11-11 18:24:33',close_user_id=NULL,close_data=NULL,", "id=20"). You can only pass 3 parameters action VARCHAR(10), IN tableName VARCHAR(50), IN columnData VARCHAR(4000)
– SneepS NinjA
the error that appeared here in my test was Error Code: 1318. Incorrect number of Arguments for PROCEDURE local.sp_syncTable; expected 3, got 5 0.000 sec
– SneepS NinjA
Hello, have an example recursive callin that hilum
– Marcos Lozina