mysql create stored procedure . what wrong with my code? - mysql

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 ;

Related

ERROR 1064 in mysql, can anybody check what is the error?

CREATE PROCEDURE insert_user(in uname varchar(20),in gender varchar(20),in email varchar(20),in phone varchar(20),in pword varchar(20),in city varchar(20))
BEGIN
DECLARE finished integer default 0;
Declare cnt integer default 0;
declare id integer;
DECLARE c_cur cursor for select user_id from user;
DECLARE CONTINUE HANDLE FOR NOT FOUND SET finished = 1;
open c_cur;
ins_user: loop
fetch c_cur into id;
IF finished = 1 THEN
LEAVE ins_user;
end if;
cnt:=id;
end loop ins_user;
cnt:=cnt+1;
insert into user
values(cnt,uname,email,phone,city,pword,gender);
END;
Am getting the 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 'HANDLE FOR NOT FOUND SET finished = 1; open c_cur; ins_user: loop fetch ' at line 7
i am not sure where its getting wrong
Set the Delimiter to something else, other than ;, for eg: $$. This will allow the parser to treat the whole create statement as one.
At the end, redefine the Delimiter back to ;
user is a keyword in MySQL. It is better to rename your table to something else, or use backticks around it.
There is a typo; it should be HANDLER not HANDLE
Try:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_user $$
CREATE PROCEDURE insert_user(in uname varchar(20),
in gender varchar(20),
in email varchar(20),
in phone varchar(20),
in pword varchar(20),
in city varchar(20))
BEGIN
DECLARE finished INT DEFAULT 0;
Declare cnt INT DEFAULT 0;
declare id INT;
DECLARE c_cur CURSOR FOR select user_id from `user`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
open c_cur;
ins_user: loop
fetch c_cur into id;
IF finished = 1 THEN
LEAVE ins_user;
end if;
SET cnt:=id; -- set was missing here
end loop ins_user;
SET cnt:=cnt+1; -- set was missing here
insert into `user`
values(cnt,uname,email,phone,city,pword,gender);
END $$
DELIMITER ;

Error in if...end-if stored procedure in mysql

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 ;

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;

MySQL 5.6 Declare Issue

Let's see if I can edit this and put the whole procedure in.
I am trying to convert an Oracle database to MySQL. I have all the tables, keys, indexes, and views converted. I now need to convert a stored procedure to MySQL.
I have most of it done, and there is only one hang up on my code:
set dns1_tmp = X.X.X.X;
SET dns2_tmp = X.X.X.X;
This gives me an error of 1064 Syntax Error: Missing semicolon
I have tested the rest of my procedure, and it works fine. It creates it, runs it, and retrieves data from it, but only if I remove those two lines.
Any ideas on what I can do?
Whole stored procedure:
DELIMITER //
USE `TEST`//
DROP PROCEDURE IF EXISTS `proc_IN`//
CREATE DEFINER=`root`#`localhost` PROCEDURE `proc_IN`
(IN DNIS VARCHAR(20),
IN MSISDN VARCHAR(20),
IN AVPAIR1 VARCHAR(20),
IN AVPAIR2 VARCHAR(20),
IN GROUPID VARCHAR(20),
OUT DNS1 VARCHAR(15),
OUT DNS2 VARCHAR(15),
OUT AUTHSTAT VARCHAR(100))
BEGIN
declare dns1_tmp varchar(15);
declare dns2_tmp varchar(15);
set dns1_tmp = X.X.X.X;
SET dns2_tmp = X.X.X.X;
DECLARE avpair1_tmp varchar(15);
DECLARE avpair2_tmp varchar(15);
DECLARE grpid_tmp varchar(15);
DECLARE C_USER CURSOR FOR SELECT AVPAIR1, AVPAIR2, DNS1, DNS2, GROUPID FROM GRP, ALLMEMBER WHERE ALLMEMBER.GROUPID=GRP.GROUPID
UNION
SELECT AVPAIR1, AVPAIR2, DNS1, DNS2, GROUPID FROM GRP;
OPEN C_USER;
FETCH C_USER INTO AVPAIR1, AVPAIR2, DNS1, DNS2, GROUPID;
LOOP
FETCH C_USER INTO avpair1_tmp, avpair2_tmp, dns1_tmp, dns2_tmp, grpid_tmp;
INSERT INTO duplog VALUES(DNIS, MSISDN, avpair1_tmp, avpair2_tmp, dns1_tmp,dns2_tmp, grpid_tmp, SYSDATE);
END LOOP;
IF C_USER%ROWCOUNT > 1 THEN
INSERT INTO duplog VALUES(DNIS, MSISDN, AVPAIR1, AVPAIR2, DNS1,DNS2, GROUPID, SYSDATE);
SET AUTHSTAT := 'ok';
elseif C_USER%ROWCOUNT = 1 THEN
SET AUTHSTAT := 'ok';
ELSE
SET AUTHSTAT := NULL;
END IF;
CLOSE C_USER;
COMMIT;
END //
DELIMITER ;

mysql procedure using cursor

DELIMITER $$
CREATE PROCEDURE INSERT_NONE_HISTORY_CHECKBOX()
BEGIN
DECLARE note_id bigint(20);
FOR c1 IN
(SELECT question_id
FROM question_master
WHERE question_type LIKE '%check box%')
LOOP
SELECT note_section_id INTO note_id
FROM answer_master
WHERE question_id = c1.question_id
LIMIT 1;
INSERT INTO answer_master(QUESTION_ID, NOTE_SECTION_ID, ANSWER_TEXT
, ROS_INPUT_TEXT, HAS_CHILD_QUES, MEDICATIONS_LIST_ID, STATUS_CODE)
VALUES(c1.question_id,note_id,'none',null,0,null,1);
END LOOP;
END $$
DELIMITER ;
i am getting error like ::
Script line: 3 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 c1 in (select question_id >from question_master where question_type like '%ch' at line 6
What am I doing wrong?
I don't think MySQL supports the FOR IN syntax you'll have to declare a cursor and loop using that.
DELIMITER $$
CREATE PROCEDURE INSERT_NONE_HISTORY_CHECKBOX()
BEGIN
DECLARE note_id bigint(20);
DECLARE Myquestion_id INTEGER;
DECLARE done BOOLEAN DEFAULT 0; //loop variable
DECLARE cur1 CURSOR FOR
SELECT question_id
FROM question_master
WHERE question_type LIKE '%check box%'; //declare the cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; //stop when done.
OPEN cur1; //Open it.
insert_loop: LOOP
FETCH cur1 INTO myquestion_id;
IF done THEN LEAVE insert_loop; END IF;
SELECT note_section_id INTO note_id
FROM answer_master
WHERE question_id = c1.question_id
LIMIT 1;
INSERT INTO answer_master(QUESTION_ID, NOTE_SECTION_ID, ANSWER_TEXT
, ROS_INPUT_TEXT, HAS_CHILD_QUES, MEDICATIONS_LIST_ID, STATUS_CODE)
VALUES(myquestion_id,note_id,'none',null,0,null,1);
END LOOP;
CLOSE cur1;
END $$
DELIMITER ;
The syntax is a bit cumbersome, but this should work.
See: http://dev.mysql.com/doc/refman/5.0/en/cursors.html