MySQL / MariaDB troubleshooting stored procedure syntax error - mysql

I am trying to write the following stored procedure but I keep getting a syntax error, which I've included under the SP.
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `test`(categoryID int, userID int)
BEGIN
DECLARE vbUserId INT DEFAULT ( SELECT userfield.field6 FROM userfield WHERE userfield.field6 = userID );
DECLARE m_forumId, m_numOfPosts, m_numOfThreads, m_hasChildren, m_isPrivate INT;
DECLARE m_forumName VARCHAR(100);
DECLARE lastRow INT DEFAULT 0;
CREATE TEMPORARY TABLE tmp engine=memory AS (select forumid,replace(replace(title_clean,'&','&'),'"','') as forumName,replycount as NumOfPosts, threadcount as NumOfThreads, 0 as hasChildren, showprivate as isprivate from forum where parentid=categoryID and displayorder!=0 and options&1=1 order by displayorder);
DECLARE cur_forums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET lastRow = 1;
OPEN cur_forums;
iterate_forums: LOOP
FETCH cur_forums INTO m_forumId, m_forumName, m_numOfPosts, m_numOfThreads, m_hasChildren, m_isPrivate;
IF lastRow = 1 THEN LEAVE iterate_forums;
IF (m_isPrivate = 1)
SELECT CONCAT('Private: ', m_isPrivate);
END IF
END LOOP iterate_forums;
CLOSE cur_forums;
DROP TEMPORARY TABLE IF EXISTS tmp;
END$$
Here is the error I receive when I try to import this into a db:
ERROR 1064 (42000) at line 2: 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 'DECLARE cur_forums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR N' at line 10
I've read the documentation and looked over previous SO questions, and also tried commenting out certain lines or running them on their own, but still can't figure out what I'm doing wrong.

Chuck tmp and the cursor. Instead, have simply
select "Private: 1"
FROM forum
WHERE EXISTS ( SELECT *
from forum
where parentid=categoryID
and displayorder!=0
and options&1=1
AND showprivate = 1 );
vbUserId seems to be unused; get rid of it.

change the order like: 1 declare, then open
....
DECLARE vbUserId INT DEFAULT ( SELECT userfield.field6 FROM userfield WHERE userfield.field6 = userID );
DECLARE m_forumId, m_numOfPosts, m_numOfThreads, m_hasChildren, m_isPrivate INT;
DECLARE m_forumName VARCHAR(100);
DECLARE lastRow INT DEFAULT 0;
DECLARE cur_forums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET lastRow = 1;
OPEN cur_forums;
CREATE TEMPORARY TABLE tmp engine=memory AS (select forumid,replace(replace(title_clean,'&','&'),'"','') as .....
...

Fixed it by re-writing as so:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `test`(categoryID int, userID int)
BEGIN
DECLARE vbUserId INT DEFAULT (
SELECT userfield.field6 FROM userfield
WHERE userfield.field6 = userID );
DECLARE m_forumId, m_numOfPosts, m_numOfThreads,
m_hasChildren, m_isPrivate INT;
DECLARE m_forumName VARCHAR(100);
DECLARE lastRow INT DEFAULT FALSE;
DECLARE curForums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET lastRow = TRUE;
CREATE TEMPORARY TABLE tmp engine=memory AS (
select forumid,
replace(replace(title_clean,'&','&'),'"','')
as forumName,
replycount as NumOfPosts,
threadcount as NumOfThreads,
0 as hasChildren,
showprivate as isprivate
from forum
where parentid=categoryID
and displayorder!=0
and options&1=1
order by displayorder);
OPEN curForums;
iterateForums: LOOP
FETCH curForums INTO m_forumId, m_forumName,
m_numOfPosts, m_numOfThreads,
m_hasChildren, m_isPrivate;
IF lastRow THEN
LEAVE iterateForums;
END IF;
IF m_isPrivate = 1 THEN
SELECT (m_isPrivate);
END IF;
END LOOP;
CLOSE curForums;
DROP TEMPORARY TABLE IF EXISTS tmp;
END$$
DELIMITER ;

Related

Error Code: 1064. You have an error in your SQL syntax; for the right syntax to use near 'DECLARE #maxdate DATETIME = (SELECT Max [duplicate]

I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;

Using Loop and Cursor in same mysql procedure showing error

The following code is working properly. But when i am enabling the commented area (cursor), then the code showing error. Please help to fix the issue.
Scenario: The code allow some parameter. It will prepare the data in a table and then the cursor will take data from that table and output that data.
Same Parameter: call prGetInsuranceData_Multiple(2, 'Saroar,Ahmed', '20,30')
DELIMITER $$
USE `surokkha_db`$$
DROP PROCEDURE IF EXISTS `prGetInsuranceData_Multiple`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `prGetInsuranceData_Multiple`
(
PeopleToBeCovered INT,
IN NAME VARCHAR(4000),
IN AGE VARCHAR(200)
)
BEGIN
-- declare loop variables
DECLARE V_Name VARCHAR(255);
DECLARE V_AGE INT;
DECLARE X INT DEFAULT 0;
-- declare cursor variables
DECLARE Cur_Finished INTEGER DEFAULT 0;
DECLARE Cur_Name VARCHAR(255);
DECLARE Cur_Age INT;
-- create a table with comma separated values (Name with age in table format)
CREATE TEMPORARY TABLE TempCustomer
(
NAME VARCHAR(255),
AGE INT
);
CREATE TEMPORARY TABLE TempCustomer1
(
NAME VARCHAR(255),
AGE INT
);
SET X = 1;
BEGIN
WHILE X <= PeopleToBeCovered DO
SET V_Name = SUBSTRING_INDEX(SUBSTRING_INDEX(NAME,',',X),',',-1);
SET V_Age = SUBSTRING_INDEX(SUBSTRING_INDEX(AGE,',',X),',',-1);
SET X = X + 1;
INSERT INTO TempCustomer VALUES(V_Name, V_Age);
END WHILE;
END;
/*
-- declare cursor
DECLARE cur_NameWithAge
CURSOR FOR
SELECT NAME, AGE FROM TempCustomer;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN cur_NameWithAge;
GetNameWithAge: LOOP
FETCH cur_NameWithAge INTO Cur_Name, Cur_Age;
IF finished = 1 THEN
LEAVE GetNameWithAge;
END IF;
-- get data and insert into table
INSERT INTO TempCustomer1 VALUES(Cur_Name, Cur_Age);
END LOOP GetNameWithAge;
CLOSE cur_NameWithAge;
*/
SELECT * FROM TempCustomer;
-- as after setting cursor the data is not needed, thats why drop the tables
DROP TEMPORARY TABLE TempCustomer;
DROP TEMPORARY TABLE TempCustomer1;
END$$
DELIMITER ;
You need to pout the loop and its declarations in a BEGIn ENd
Still i needed to reprogram the hole thing, because your code threw error, that i couldn't find
create procedure prGetInsuranceData_Multiple(
IN PeopleToBeCovered INT,
IN _NAME VARCHAR(4000),
IN _AGE VARCHAR(200))
begin
DECLARE V_Name VARCHAR(255);
DECLARE V_AGE INT;
DECLARE X INT DEFAULT 0;
drop temporary table if exists TempCustomer;
drop temporary table if exists TempCustomer1;
create temporary table TempCustomer( NAME VARCHAR(255),AGE int );
create temporary table TempCustomer1(NAME VARCHAR(255),AGE int);
SET X = 1;
BEGIN
WHILE X <= PeopleToBeCovered DO
SET V_Name = SUBSTRING_INDEX(SUBSTRING_INDEX(_NAME,',',X),',',-1);
SET V_Age = SUBSTRING_INDEX(SUBSTRING_INDEX(_AGE,',',X),',',-1);
SET X = X + 1;
INSERT INTO TempCustomer VALUES(V_Name,V_Age);
END WHILE;
END;
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE v_id int ;
DECLARE v_name varchar(255);
declare cur_NameWithAge cursor for select NAME,AGE from TempCustomer;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
open cur_NameWithAge;
GetNameWithAge: LOOP
fetch cur_NameWithAge into v_name,v_id;
IF finished = 1 THEN
LEAVE GetNameWithAge;
END IF;
INSERT INTO TempCustomer1 VALUES (v_name,v_id);
END LOOP GetNameWithAge;
CLOSE cur_NameWithAge;
END;
select * FROM TempCustomer;
DROP TEMPORARY TABLE TempCustomer1;
DROP TEMPORARY TABLE TempCustomer1;
end
call prGetInsuranceData_Multiple(2, 'Saroar,Ahmed', '20,30')
NAME | AGE
:----- | --:
Saroar | 20
Ahmed | 30
db<>fiddle here

Cursor to copy distinct record from one table to another

I want to copy all recoreds from temp1 table to anoter two tables I am using cursor for this .
DELIMITER //
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
declare tcursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;
OPEN tcursor;
REPEAT
FETCH cursor into v_fn,v_ln, v_email;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fname,v_lname,v_email);
END REPEAT;
CLOSE tcursor;
END//
DELIMITER
this code is showing error
MySQL said: Documentation
#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 'for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLE' at line 8
How to resolve this
You have multiple errors in your syntax and don't exit the loop. Try this?
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
DECLARE done INT DEFAULT FALSE;
declare tcursor cursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN tcursor;
read_loop: LOOP
FETCH tcursor into v_fn,v_ln, v_email;
if done then
LEAVE read_loop;
END IF;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fn,v_ln,v_email);
END LOOP;
CLOSE tcursor;
END
I tried this query and find this is working
insert into atom(id,type) select id,'Person' from user1;
INSERT INTO user( id, fname, lname, mailid ) SELECT id, fname, lname, mailid FROM user1;

Declare variable syntax invalid in MySQL Workbench?

I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;

cursor not working in stored procedure it throwing 1064 error

I want to use cursor in my project but it throwing a error 1064. Please help me in resolving the problem.....
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 curs1 CURSOR FOR SELECT healthinsurancepremium.productid ,healthinsuran' at line 63
My Procedure code:
CR-EATE DEFINER=`root`#`localhost` PROCEDURE `spGettestOffline` (
IN in_sumassured INT(10),
IN in_age INT(3),
IN in_adult INT(4),
IN in_child INT(4),
IN in_tenure INT(3),
IN in_city VARCHAR(20)
)
BEGIN
DECLARE bDone INT DEFAULT 0;
DECLARE var1 INT ;
DECLARE Var2 INT;
DECLARE Var3 INT;
DECLARE var4 INT;
/* this is the table declaration */
DROP TEMPORARY TABLE IF EXISTS tblResults;
CREATE TEMPORARY TABLE IF NOT EXISTS tblResults (
productid INT,
suminsured INT,
amount INT,
tenent INT
);
/* this is the cursor declaration */
DECLARE curs1 CURSOR FOR SELECT healthinsurancepremium.productid ,healthinsurancepremium.suminsured ,healthinsurancepremium.amount ,healthinsurancepremium.tenure FROM healthinsurancepremium LEFT JOIN `cityspecifichealthpremium` ON `healthinsurancepremium`.`id` >= `cityspecifichealthpremium`.`healthpremiumidmin` AND `healthinsurancepremium`.`id` <= `cityspecifichealthpremium`.`healthpremiumidmax` WHERE (`cityspecifichealthpremium`.`healthpremiumidmax` IS NULL AND `cityspecifichealthpremium`.`healthpremiumidmin` IS NULL) AND healthinsurancepremium.suminsured = in_sumassured AND in_age >=healthinsurancepremium.minage AND in_age <=healthinsurancepremium.maxage AND healthinsurancepremium.adult=in_adult AND healthinsurancepremium.child=in_child ;
/* this is the cursor looping */
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
OPEN curs1;
read_loop: LOOP
FETCH curs1 INTO var1,var2,var3,var4;
INSERT INTO tblResults VALUES (var1,var2, var3,var4);
IF (bDone = 1) THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE curs1;
SELECT * FROM tblResults;
END$$
I think this problem belongs to the cursor declaration part.... but I did not find anything on it
You have put the DECLARE statement at a wrong place.
Usage condition on DECLARE statements:
You must DECLARE them explicitly at the start of the BEGIN/END block, along with their data types.
Move all your DECLARE ... statements to start of the BEGIN block.
It should look like:
BEGIN
DECLARE bDone INT DEFAULT 0;
DECLARE var1 INT ;
DECLARE Var2 INT;
DECLARE Var3 INT;
DECLARE var4 INT;
DECLARE curs1 CURSOR FOR
SELECT
healthinsurancepremium.productid,
healthinsurancepremium.suminsured,
healthinsurancepremium.amount,
healthinsurancepremium.tenure
FROM healthinsurancepremium
LEFT JOIN `cityspecifichealthpremium` ON
`healthinsurancepremium`.`id` >= `cityspecifichealthpremium`.`healthpremiumidmin` AND
`healthinsurancepremium`.`id` <= `cityspecifichealthpremium`.`healthpremiumidmax`
WHERE
( `cityspecifichealthpremium`.`healthpremiumidmax` IS NULL AND
`cityspecifichealthpremium`.`healthpremiumidmin` IS NULL ) AND
healthinsurancepremium.suminsured = in_sumassured AND
in_age >= healthinsurancepremium.minage AND
in_age <= healthinsurancepremium.maxage AND
healthinsurancepremium.adult = in_adult AND
healthinsurancepremium.child = in_child ;
Refer to: Stored Procedures - MySQL