Syntax error when calling stored procedure inside stored procedure - mysql

I'm trying to call a stored procedure inside another stored procedure.
I'm getting a syntax error in the statement where main procedure calls the child procedure.
here is my query
DROP PROCEDURE GetAllProducts;
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
DECLARE finished INT DEFAULT 0;
DECLARE myName varchar(60);
DEClARE myCursor CURSOR FOR
SELECT Name FROM Test;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished =1 ;
OPEN myCursor;
get_email: LOOP
FETCH myCursor INTO myName;
IF finished = 1 THEN
LEAVE get_email;
END IF;
-- Line which shows a syntax error
EXEC UpdateProdcut myName;
END LOOP get_email;
CLOSE myCursor;
DEALLOCATE myCursor;
END //
DELIMITER ;
This is the error message
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax
to use near 'UpdateProdcut myName OUTPUT;
mysql version is 5.5.54
any idea about this ?
Thanks.

Use CALL instead:
call UpdateProdcut(myName);
execute is used for prepared statements.
Ref:
https://dev.mysql.com/doc/refman/5.5/en/call.html

Related

MariaDB syntax error when using IF statements

I'm trying to create a stored procedure in MariaDB using the code below.
DELIMITER //
CREATE PROCEDURE P5();
BEGIN
IF 1=1 THEN
SELECT 1;
END IF;
END//
When I run the code, I receive a syntax error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ';
BEGIN
IF 1=1 THEN
SELECT 1;
END IF;
END' at line 1
I realise that I could use an IF () function in this case, but I need to be able to use IF statements.
No need of adding semicolon after procedure name.
DELIMITER //
CREATE PROCEDURE P5()
BEGIN
IF 1=1 THEN
SELECT 1;
END IF;
END//
Check db fiddle
Refer: MySQL Docs

How to call procedure inside cursor in MariaDB

I'm new to MariaDB, I only have some experience in MS SQL Server.
I'm trying to call a procedure for every row returned from a query.
But I can't find the error when trying to create a procedure with a cursor.
I'm using Heidy to connect to MariaDB 10.0
CREATE PROCEDURE SetAll()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE id INT ;
DECLARE cur CURSOR FOR SELECT aID FROM Customers ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN cur;
testLoop: LOOP
FETCH cur INTO id;
IF done THEN
LEAVE testLoop;
END IF;
CALL SetById(id);
END LOOP testLoop;
CLOSE cur;
END
Error: "/* Error de SQL (1064): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 4 */"
My precedure SetById(id) work just fine.

Exception using TRY CATCH in mysql stored procedure

Following is the procedure in which i am trying to catch the exception, if there is any in stored procedure
DROP PROCEDURE IF EXISTS test;
DELIMITER //
CREATE PROCEDURE test( IN contextFilter TEXT)
BEGIN TRY
set #sub_query = 'SELECT id from test_table';
PREPARE stmt_query FROM #sub_query;
SELECT #sub_query;
EXECUTE stmt_query;
DEALLOCATE PREPARE stmt_query;
END TRY //
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_MESSAGE() AS ErrorMessage;
END CATCH
DELIMITER ;
When i try to source the stored procedure , i am getting the following exception
source /home/test.sql;
Query OK, 0 rows affected (0.01 sec)
ERROR 1064 (42000) at line 5 in file: '/home/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'set #sub_query = 'SELECT id from test_table';
PREPARE stmt_query FROM #' at line 5
ERROR 1064 (42000) at line 17 in file: '/home/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CATCH
SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_MESSAGE() AS ErrorMessage;
' at line 1
The post above helped me, but I still had to do some research on my own. In order to provide a complete example you can see below. This code will declare a handler for "catching" SQLExceptions and then exit if it occurs.
DROP PROCEDURE IF EXISTS `SP_TEST`;
DELIMITER //
CREATE PROCEDURE `SP_TEST`()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 #p1 = RETURNED_SQLSTATE, #p2 = MESSAGE_TEXT;
SELECT CONCAT(#p1, ':', #p2);
END;
SELECT FAKE_COLUMN FROM MY_TABLE;
END //
CALL SP_TEST;
This works in mySQL version 5.7 and hope it helps you.
In mySql, there is no try... catch block like in Java. You can I handle errors through error handling?
Syntax
DECLARE handler_action HANDLER
FOR condition_value [, condition_value] ...
statement
handler_action:
CONTINUE
| EXIT
| UNDO
condition_value:
mysql_error_code
| SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
The handler_action value indicates what action the handler takes after execution of the handler statement:
CONTINUE: Execution of the current program continues.
EXIT: Execution terminates for the BEGIN ... END compound statement in which the handler is declared. This is true even if the condition occurs in an inner block.
UNDO: Not supported.
For more info go to Official site

Mysql error 1064 in Mysql 5.1- unable execute successfully

I am trying to write trigger in Mysql (5.1), but getting following error, please help.
The error is:
SQL Error (1064): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '' at line 5.
Purpose for writing trigger:
I am writing application where I am assigning users, and I want to store unassigned usercount to field cluster_count in IX_branchdetails table.After updating the base table.
trigger:
DELIMITER $$
CREATE TRIGGER upd_trg AFTER
UPDATE ON DBNAME.BASETABLE
FOR EACH ROW
BEGIN
DECLARE m_branchcode INTEGER;
DECLARE cnt INTEGER;
DECLARE cursor_branch CURSOR FOR
SELECT DISTINCT branchcode
FROM ix_branchdetails;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cursor_branch;
my_loop: loop
set done = false;
fetch cursor_branch into m_branchcode;
if done then
leave my_loop;
end if;
select count(1) into cnt from (select count(1) from BASETABLE Where IX_BRANCHCODE = m_branchcode) as temp;
update DBANAME.ix_branchdetails set DBANAME.ix_branchdetails.cluster_count = cnt where DBANAME.ix_branchdetails.BRANCHCODE = m_branchcode;
end loop my_loop;
close cursor_branch;
END $$
DELIMITER ;
I don't see a declare for the done variable:
DECLARE done TINYINT DEFAULT FALSE;
The semicolon (;) is the default delimiter for MySQL statements. To get a procedure/function/trigger defined, we normally see the statement delimiter changed to a string that doesn't appear in the statement:
DELIMITER $$
CREATE PROCEDURE ...
END$$
DELIMITER ;
If the delimiter is not changed from the semicolon, then when MySQL encounters the first semicolon in your procedure/function/trigger, it sees that as the end of the statement, which is not what you want. You want MySQL to see the entire block of code as a single statement.

EXECUTE in MySQL Stored Procedure gives syntax error

I'm new to MySQL and databases in general. I'm trying to create a MySQL stored procedure but keep getting a vague syntax error:
"you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sps; EXECUTE stmt USING nn,ee,ll,cc,uu; COMMIT END' at line 11"
If I remove the line "PREPARE stmt ..." then the stored procedure is created. When I put the line back in, I get the error again.
What is it that I am doing wrong?
DELIMITER //
CREATE PROCEDURE `account_create` (nn VARCHAR(25),
ee BIGINT,
ll BIGINT,
cc VARCHAR(100),
uu VARCHAR(25))
BEGIN
DECLARE newId BIGINT;
DECLARE sps VARCHAR(50);
START TRANSACTION;
set sps = 'INSERT INTO account SET name=?, entity=?, ledger=?, tblname=tmpXXX, creation_date=CURDATE(), comment=?, uname=?';
PREPARE stmt FROM sps;
COMMIT;
END//
You must use a User Defined Variable to execute a prepared statement. Rewrite as:
...
BEGIN
DECLARE newId BIGINT;
START TRANSACTION;
set #sps = 'INSERT INTO account SET name=?, entity=?, ledger=?, tblname=tmpXXX, creation_date=CURDATE(), comment=?, uname=?';
PREPARE stmt FROM #sps;
COMMIT;
END//