drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
FETCH curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
CLOSE curs1;
end ;
;;
delimiter ;
above throws an error : SQL 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 '' at line 14
create table test11(abc varchar(20), cde varchar(20), eg varchar(20), asda varchar(20));
insert into test11 values("Hello",'1989-08-09','sdads','22');
create table test1(abc varchar(20), cde int(20), eg date, asda varchar(20));
I don't what i am doing wrong. Syntax seems to be fine.
drop procedure if exists sp;
CREATE PROCEDURE sp()
BEGIN
DECLARE intoffer INT;
SELECT max(asda) INTO intoffer FROM test11;
IF (intoffer IS NULL) THEN
SET intoffer = 1;
ELSE
SET intoffer = intoffer + 1;
END IF;
INSERT INTO test1 (asda) VALUES (intoffer);
end;
This is another procedure throwing same error. After removing the if..end-if both the code seems to work fine.
You are Missing fetch from
drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
insertLoop: LOOP
FETCH FROM curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
END LOOP insertLoop;
CLOSE curs1;
end ;
;;
delimiter ;
Related
Error
SQL query:
CREATE PROCEDURE GEN_MFREE( ) BEGIN ;
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 '' at line 2
what wrong with my code ?
at below is my code :
CREATE PROCEDURE GEN_MFREE()
BEGIN
DECLARE CODE VARCHAR (10);
DECLARE BLOCK VARCHAR (10);
DECLARE UNIT VARCHAR (10);
DECLARE FLOOR VARCHAR (10);
DECLARE FIRSTNAME VARCHAR(10);
DECLARE LASTNAME VARCHAR(10);
DECLARE AMT DECIMAL(18,2) ;
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_i CURSOR FOR SELECT
B_resident.CODE,
B_resident.BLOCK,
B_resident.UNIT,
B_resident.FLOOR,
B_resident.FIRSTNAME,
B_resident.LASTNAME,
B_resident.TEL,
B_ResManFree.SIZE * B_ResManFree.FREE AS AMT,
'2016-01-01' AS MDATE
FROM B_resident LEFT OUTER JOIN B_ResManFree ON
B_resident.UNIT = B_ResManFree.UNIT AND
B_resident.BLOCK = B_ResManFree.BLOCK
WHERE B_resident.MAIN_CONT ='YES'
ORDER BY B_resident.BLOCK,B_resident.FLOOR,B_resident.UNIT
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_i;
read_loop: LOOP
FETCH cursor_i INTO CODE, BLOCK, UNIT, FLOOR, FIRSTNAME, LASTNAME, AMT ;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO B_MfreeStatment(RES_CODE,
RES_BLOCK,
RES_UNIT,
RES_FLOOR,
BAN_CODE,
RES_FIRSTNAME,
RES_LASTNAME,
AMT,MDATE)
VALUES( CODE, BLOCK, UNIT, FLOOR,'001' FIRSTNAME, LASTNAME, AMT,'2016-01-01' );
END LOOP;
CLOSE cursor_i;
END;
;;
You miss to set the delimiter first:
Delimiter //
--> Your Code
--> End Code with // instead of ;;
Delimiter ;
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;
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 ;
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
can anyone help me on why the I can't create the temporary table on the stored procedure.
Here is my stored procedure script
DELIMITER $$
DROP PROCEDURE IF EXISTS getProductionItem$$
CREATE DEFINER=pvtuser#`%` PROCEDURE getProductionItem(projectID BIGINT(20))
BEGIN
CREATE TEMPORARY TABLE tempProdItem(
prodType SET('JOB','BATCH','DOCGROUP'),
taskQueueId BIGINT(20),
jobBatchDocId BIGINT(20),
jobBatchDocName VARCHAR(255),
lockStatus SET('LOCKED','OPEN')
);
DECLARE done INT DEFAULT 0;
DECLARE prodType VARCHAR(10);
DECLARE taskQueueId BIGINT(20);
DECLARE jobBatchDocId BIGINT(20);
DECLARE jobBatchDocName VARCHAR(255);
DECLARE lockStatus VARCHAR(10);
DECLARE docCursor CURSOR
FOR SELECT 'DOCGROUP',
jq.taskqueueid,
jq.documentgroupid,
dgd.documentGroupName,
COALESCE(jql.lockStatus,'OPEN')
FROM jobdetails jd
INNER JOIN jobqueue jq
ON jd.jobid=jq.jobid
INNER JOIN documentgroupdetails dgd
ON jq.documentgroupid=dgd.documentgroupid
LEFT JOIN jobqueuelocked jql
ON jq.taskqueueid=jql.taskqueueid
GROUP BY jq.taskqueueid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN docCursor;
Loop1: LOOP
FETCH docCursor INTO prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus;
IF done=1 THEN
LEAVE Loop1;
ELSE
/* TODO INSERT HERE IN TEMP TABLE */
END IF;
END LOOP;
CLOSE docCursor;
SELECT * FROM tempProdItem;
END
$$
DELIMITER;
DELIMITER $$
DROP PROCEDURE IF EXISTS getProductionItem$$
CREATE DEFINER=pvtuser#`%` PROCEDURE getProductionItem(projectID BIGINT(20))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE prodType VARCHAR(10);
DECLARE taskQueueId BIGINT(20);
DECLARE jobBatchDocId BIGINT(20);
DECLARE jobBatchDocName VARCHAR(255);
DECLARE lockStatus VARCHAR(10);
DECLARE docCursor CURSOR
FOR SELECT 'DOCGROUP',
jq.taskqueueid,
jq.documentgroupid,
dgd.documentGroupName,
COALESCE(jql.lockStatus,'OPEN')
FROM jobdetails jd
INNER JOIN jobqueue jq
ON jd.jobid=jq.jobid
INNER JOIN documentgroupdetails dgd
ON jq.documentgroupid=dgd.documentgroupid
LEFT JOIN jobqueuelocked jql
ON jq.taskqueueid=jql.taskqueueid
GROUP BY jq.taskqueueid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
CREATE TEMPORARY TABLE tempProdItem(
prodType SET('JOB','BATCH','DOCGROUP'),
taskQueueId BIGINT(20),
jobBatchDocId BIGINT(20),
jobBatchDocName VARCHAR(255),
lockStatus SET('LOCKED','OPEN')
);
OPEN docCursor;
Loop1: LOOP
FETCH docCursor INTO prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus;
IF done=1 THEN
LEAVE Loop1;
ELSE
INSERT INTO tempProdItem
(prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus)
VALUES (prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus);
END IF;
END LOOP;
CLOSE docCursor;
SELECT * FROM tempProdItem;
END
$$
DELIMITER;