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 ;
Related
I'm trying to make a Nested Cursor in Mysql by following this instruction.
Then i got this issue:
#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 'DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;' at line 22
I've 2 table 'account' and 'n_activity' (n = account_id in table 'account')
Ex: i've table 'account' and '20_activity'.
So i want to loop the 'account_id' and get the 'activity_id' from that loop.
Here is my code:
DROP PROCEDURE if exists update_schema_activity_startdate_and_duedate;
DELIMITER $$
CREATE PROCEDURE update_schema_activity_startdate_and_duedate()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE accountid INT;
--
-- GET ALL ACCOUNT ID
--
DECLARE accountids CURSOR FOR SELECT account_id FROM account;
--
-- LOOP
--
OPEN accountids;
read_loop: LOOP
FETCH accountids INTO accountid;
BLOCK2: BEGIN
SET #_activity = CONCAT(accountid,'_activity');
DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;
END LOOP;
CLOSE accountids;
END$$
DELIMITER ;
CALL update_schema_activity_startdate_and_duedate();
Please help, thanks.
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 have a mysql function that returns a varchar value. Inside this function I have define a cursor which only gives a single value. This means in my select statement, I have taken a specific value using table primary key combination. Since I know that this cursor only return one value I don't want to add a loop to check whether the cursor return a value or not.
DELIMITER //
CREATE FUNCTION PROGRAM_API_Get_Name(
program_id_ VARCHAR(15),
uni_id_ VARCHAR(15),
fac_id_ VARCHAR(15)) RETURNS VARCHAR(100)
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE degree_name_ VARCHAR(100);
DECLARE get_name_ CURSOR FOR
SELECT program_name
FROM degree_program_tab
WHERE program_id = program_id_
AND uni_id = uni_id_
AND fac_id = fac_id_;
OPEN get_name_;
IF(!done) THEN
FETCH get_name_ INTO degree_name_;
CLOSE get_name_;
RETURN degree_name_;
END IF;
RETURN NULL;
END//
This function gives me the following error
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 'BEGIN DECLARE CONTINUE HANDLER FOR
NOT FOUND SET done = TRUE; ' at line 5 0.
If you know how to overcome this, Please help me
I found an answer for this,
cursor.rowcount is the solution
DELIMITER //
CREATE FUNCTION PROGRAM_API_Get_Name(
program_id_ VARCHAR(15),
uni_id_ VARCHAR(15),
fac_id_ VARCHAR(15)) RETURNS VARCHAR(100)
BEGIN
DECLARE degree_name_ VARCHAR(100);
DECLARE get_name_ CURSOR FOR
SELECT program_name
FROM degree_program_tab
WHERE program_id = program_id_
AND uni_id = uni_id_
AND fac_id = fac_id_;
OPEN get_name_;
IF(get_name_.rowcount>0) THEN
FETCH get_name_ INTO degree_name_;
CLOSE get_name_;
RETURN degree_name_;
END IF;
RETURN NULL;
END//
Maybe you can avoid the cursor with a function as:
DELIMITER //
DROP FUNCTION IF EXISTS `PROGRAM_API_Get_Name`//
CREATE FUNCTION `PROGRAM_API_Get_Name` (
`program_id_` VARCHAR(15),
`uni_id_` VARCHAR(15),
`fac_id_` VARCHAR(15)
)
RETURNS VARCHAR(100)
BEGIN
RETURN (SELECT `program_name`
FROM `degree_program_tab`
WHERE `program_id` = `program_id_`
AND `uni_id` = `uni_id_`
AND `fac_id` = `fac_id_`);
END//
DELIMITER ;
I've written a function but it gives me mistake a the second line (create statement) if anyone could help me, I really appreciate:
CREATE FUNCTION GetPrefix (phone_num VARCHAR(30)) RETURNS varchar(30)
deterministic
BEGIN
DECLARE x INT;
DECLARE prefix varchar(30);
SET x = 0;
for prefix in SELECT code
FROM tab_len
while (length(phone_num)) > 0
do
if prefix<>left(phone_num, length(phone_num)-x)
then set x=x+1 ;
else return 1 ;
END while ;
END $$;
and I receive this 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 'for prefix in SELECT code
FROM tab_len while (length(phone_n' at line 9
DELIMITER $$
DROP FUNCTION IF EXISTS GetPrefix $$
CREATE FUNCTION GetPrefix
(
phone_num VARCHAR(30)
)
RETURNS varchar(30)
BEGIN
DECLARE var_x INT DEFAULT 0;
DECLARE var_prefix VARCHAR(100);
SET phone_num = IFNULL(phone_num,'');
-- your logic will go here.
return phone_num;
END$$
DELIMITER ;
SELECT GetPrefix('test');
This is right syntax to write a function in mysql.
check out the differences. Take a look Here
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 ;