Error 1064 in stored procedures - mysql

I get this error in the following stored procedure Error Code: 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 'DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; OPEN cur1; count' at line 13
DELIMITER $$
DROP PROCEDURE IF EXISTS cursor_example
$$
CREATE PROCEDURE cursor_example()
READS SQL DATA
BEGIN
DECLARE i_Name CHAR(3);
DECLARE i_SurfaceArea FLOAT(10,2);
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR
SELECT Name, SurfaceArea
FROM country
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN cur1;
country_loop:LOOP
FETCH cur1 INTO i_Name, i_SurfaceArea;
IF done=1 THEN
LEAVE country_loop;
END IF;
END LOOP country_loop;
CLOSE cur1;
END;
$$
DELIMITER ;
Just generally how the whole procedure is suppose to work and what is it for.
Thanks.

You were missing a semi-colon after FROM country. Use the following pattern:
DROP PROCEDURE IF EXISTS cursor_example;
DELIMITER $$
CREATE PROCEDURE cursor_example()
READS SQL DATA
BEGIN
DECLARE i_Name CHAR(3);
DECLARE i_SurfaceArea FLOAT(10,2);
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR
SELECT Name, SurfaceArea
FROM country;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN cur1;
country_loop:LOOP
FETCH cur1 INTO i_Name, i_SurfaceArea;
IF done=1 THEN
LEAVE country_loop;
END IF;
-- right here is where you do stuff with those variables
END LOOP country_loop;
CLOSE cur1;
END;
$$
DELIMITER ;
I don't understand with the example is trying to FETCH cur1 INTO in
the example is for?
Remember that the CURSOR is just a select stmt. It can be really complicated with joins, you name it. But in the end it has a select column list. In your case it has 2 columns coming back. So the FETCH, one row at a time, brings the current row into LOCAL VARIABLES (in the respective order from the cursor list to the variables you list). You declared those LOCAL VARIABLES in your DECLAREs.
When you are out of rows, the HANDLER sets done to 1 and you bail out of the loop.
As for the DELIMITER read the last half of this answer of mine Here.
Just generally how the whole procedure is suppose to work and what is
it for?
Described above mostly. Cursors are for procedural handling of data returned. Allowing you to inject procedural thinking into solving problems. By the way they are terribly slow and should be avoided whenever possible. They are typically a crutch for devs new to SQL that can't get their head into how to do work with sets and relations. That is, the way high performance RDBMS's excel at.
That said, experienced SQL devs are known to use them for tricky situations.

Related

Call a stored procedure from the DECLARE statement when using cursors in MySQL

I am trying to use a cursor in MySQL to call a stored procedure many times. I want to call it as many times as a value for my_id exists in some temporary table, and iterate through those ids and concatenate the results.
Anyway, I'm having trouble with this part of the process:
DECLARE curs CURSOR FOR
SELECT something FROM somewhere;
I don't want to select something from somewhere. I want something like
DECLARE curs CURSOR FOR
CALL storedproc(#an_id);
Can the DECLARE statement be used to call a stored procedure? Or does it have to be associated with a SELECT only? Googling around, I'm afraid that the latter is the case.
Using a cursor requires some standard boilerplate code to surround it.
Using a cursor to call a stored procedure for each set of values from the table requires essentially the same boilerplate. You SELECT the values you want to pass, from wherever you're getting them (which could be a temporary table, base table, or view, and can include calls to stored functions) and then call the procedure with those values.
I've written an syntactically valid example of that boilerplate code, below, with comments to explain what each component is doing. There are few things I dislike more than being asked to just do something "just because" -- so everything is (hopefully) explained.
You mentioned calling the procedure with multiple values, so this example uses 2.
Note that there events that happen her are in a specific order for a reason. Variables have to be declared first, cursors have to be declared before their continue handlers, and loops have to follow all of those things. This gives an impression that there's some fairly extreme inflexibility, here, but that's not really the case. You can reset the ordering by nesting additional code inside BEGIN ... END blocks within the procedure body; for example, if you needed a second cursor inside the loop, you'd just declare it inside the loop, inside another BEGIN ... END.
DELIMITER $$
DROP PROCEDURE IF EXISTS `my_proc` $$
CREATE PROCEDURE `my_proc`(arg1 INT) -- 1 input argument; you might not need one
BEGIN
-- from http://stackoverflow.com/questions/35858541/call-a-stored-procedure-from-the-declare-statement-when-using-cursors-in-mysql
-- declare the program variables where we'll hold the values we're sending into the procedure;
-- declare as many of them as there are input arguments to the second procedure,
-- with appropriate data types.
DECLARE val1 INT DEFAULT NULL;
DECLARE val2 INT DEFAULT NULL;
-- we need a boolean variable to tell us when the cursor is out of data
DECLARE done TINYINT DEFAULT FALSE;
-- declare a cursor to select the desired columns from the desired source table1
-- the input argument (which you might or might not need) is used in this example for row selection
DECLARE cursor1 -- cursor1 is an arbitrary label, an identifier for the cursor
CURSOR FOR
SELECT t1.c1,
t1.c2
FROM table1 t1
WHERE c3 = arg1;
-- this fancy spacing is of course not required; all of this could go on the same line.
-- a cursor that runs out of data throws an exception; we need to catch this.
-- when the NOT FOUND condition fires, "done" -- which defaults to FALSE -- will be set to true,
-- and since this is a CONTINUE handler, execution continues with the next statement.
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- open the cursor
OPEN cursor1;
my_loop: -- loops have to have an arbitrary label; it's used to leave the loop
LOOP
-- read the values from the next row that is available in the cursor
FETCH NEXT FROM cursor1 INTO val1, val2;
IF done THEN -- this will be true when we are out of rows to read, so we go to the statement after END LOOP.
LEAVE my_loop;
ELSE -- val1 and val2 will be the next values from c1 and c2 in table t1,
-- so now we call the procedure with them for this "row"
CALL the_other_procedure(val1,val2);
-- maybe do more stuff here
END IF;
END LOOP;
-- execution continues here when LEAVE my_loop is encountered;
-- you might have more things you want to do here
END $$
DELIMITER ;
Can the DECLARE statement be used to call a stored proc?
Not possible and documentation is pretty clear on that
Cursor DECLARE Syntax
This statement declares a cursor and associates it with a SELECT statement that retrieves the rows to be traversed by the cursor. To fetch the rows later, use a FETCH statement. The number of columns retrieved by the SELECT statement must match the number of output variables specified in the FETCH statement.

mysql procedure: how can i use table name variables

I have built a MySQL procedure.
i have to define v_table variable within procedure
Who can help me ?
Thanks
Paolo
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur1;
igmLoop: loop
fetch cur1 into v_column,v_table;
IF done THEN
LEAVE igmLoop;
END IF;
update v_table set v_column=replace(v_column,'à','a`');
end loop igmLoop;
close cur1;
end
Neither table names, nor column names can be dynamic within a stored procedure. You need to assemble the sql statement as a string and execute it using PREPARE, EXECUTE, DEALLICATE PREPARE statements. So, basically, you will create a prepared statement out of your query and execute it.
The linked documentation contains examples as well.

How to do unlimited recursion in mysql?

I have a mysql stored procedure made it recursively, it always returned 499 rows max.
my stored procedure is moving in a tree (not a binary tree) and check the nodes if they have children and so on until it reached the leaves.
I don't know how can I convert my code into non-recursive way, I just want to ask for tow points:
how can I make an infinite recursive in mysql(mysql server version is 5.5)?
if that can't happened, how can I change my cod into non-recursive way?
CREATE PROCEDURE `get_citations`(in _pub_id int(10),in _lvl int,citation_count int)
BEGIN
DECLARE done INT DEFAULT FALSE;
declare p_id,c_count int;
declare _counter int default 1;
DECLARE cur1 CURSOR FOR SELECT pat_publn_id,cited_count from temp.a_citations
where pub_parent=_pub_id ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
insert into a_citations
(pat_publn_id ,
publn_nr ,
publn_kind,
publn_auth,
publn_date,
cited_pat_publn_id,
cited_count,
pub_lvl,
pub_parent)
(select p.pat_publn_id,p.publn_nr,p.publn_kind,p.publn_auth,p.publn_date,c.cited_pat_publn_id,
(select count(*) as cnt FROM patstat1304.tls212_citation c2 where c2.cited_pat_publn_id=c.pat_publn_id) as cited_count,_lvl as pub_lvl,_pub_id as pub_parent
from patstat1304.tls212_citation c,patstat1304.tls211_pat_publn p
where c.pat_publn_id=p.pat_publn_id and c.cited_pat_publn_id=_pub_id);
commit;
OPEN cur1;
read_loop: LOOP
fetch cur1 into p_id,c_count;
IF (c_count !=0) then
call get_citations( p_id,_lvl+1,c_count);
commit;
END if;
IF done THEN
LEAVE read_loop;
END IF;
set _counter=_counter+1;
if(_counter=citation_count) then
LEAVE read_loop;
end if;
end loop;
CLOSE cur1;
END
MySQL can not execute stored procedures with a very deep nesting.
Very soon, error ER_STACK_OVERRUN_NEED_MORE will appear.
Increasing the thread stack to go further will not work either.
To change the recursive call to a non recursive one, consider something like this:
1) Create a table named publications_to_process, with the publication and search level.
2) To start the search, insert the original publication in this table, with level 1.
3) In a loop, fetch one publication, examine the citations, and add the publications listed in the publications_to_process, incrementing the level.
4) As a bonus, for cases like:
Pub_1 --> Pub_2 --> Pub_3,
Pub_1 --> Pub_3
there is no need to add Pub_3 again to the search if it has been processed already.
In other words, the publications are more likely to be a directed graph that a tree.
5) Either make the table temporary, or consider adding a PROCESSLIST_ID column, so that different sessions (having a different CONNECTION_ID()) do not step on each other, when executing this search in parallel.

Mysql Stored Procedure no load no result

I started on stored procedures with MySQL PHPMyAdmin 5.5 3.4 after watching the tutorial found on this site I took this example (the tutorial siteduzero)
I reproduced the same result without
The problem is that the process is slow in execution, it continues loading without displaying results after 2 min I close the page, creating the procedure is done correctly. Here's the code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `AfficherMarques`(OUT `Marques` VARCHAR(25))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE Marques VARCHAR(25);
DECLARE pointeur CURSOR FOR SELECT DISTINCT marque FROM marchands GROUP BY marque;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN pointeur;
REPEAT
FETCH pointeur INTO Marques;
IF done = 0 THEN
SELECT Marques;
END IF;
UNTIL done END REPEAT;
CLOSE pointeur;
END

selecting in a loop using a cursor

I have got this stored procedure:
DELIMITER //
DROP PROCEDURE IF EXISTS cursor_example//
CREATE PROCEDURE cursor_example()
BEGIN
DECLARE niche_id INT;
DECLARE niche_name VARCHAR(100);
DECLARE curl CURSOR FOR SELECT * FROM `niche`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN curl;
my_loop:LOOP
FETCH curl INTO niche_id,niche_name;
IF done=1 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
CLOSE curl;
END//
DELIMITER ;
I want to output everything that the curl fetches. So I thought to put SELECT statement inside or outside the loop..but that wouldnt give me the results that I want. How do I get back all the sql results from that cursor.
And what are the advantages of using cursors compare to other a simple unbound - SELECT statement. I mean I could get the results that I wanted by simply USING a select statement without writing all that cursor code?