DROP PROCEDURE IF EXISTS oneTimeExcution;
CREATE PROCEDURE `oneTimeExcution`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE totalCount int(10) default 0;
DECLARE i INT DEFAULT 0;
DECLARE nowUnixTime int(10) unsigned default 0;
DECLARE id varchar(100) default null;
DECLARE cur CURSOR FOR SELECT deviceId FROM tb_sensor;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
select (UNIX_TIMESTAMP()-31622400) into nowUnixTime;
select count(*) from tb_sensor into totalCount;
OPEN cur;
FETCH NEXT FROM cur INTO id;
close cur;
read_loop: loop
IF done then
LEAVE read_loop;
END IF;
END LOOP;
end;
I currently encounter syntax errors in end if and end loops. I do not know why. pleas.. Please let me know what the problem is.
My current version of MySQL is 5.6.
Try the below code
CREATE PROCEDURE `oneTimeExcution`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE totalCount int(10) default 0;
DECLARE i INT DEFAULT 0;
DECLARE nowUnixTime int(10) unsigned default 0;
DECLARE id varchar(100) default null;
DECLARE cur CURSOR FOR SELECT deviceId FROM tb_sensor;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
select (UNIX_TIMESTAMP()-31622400) into nowUnixTime;
select count(*) from tb_sensor into totalCount;
OPEN cur;
read_loop: loop
FETCH NEXT FROM cur INTO id;
IF done then
LEAVE read_loop;
END IF;
select 'success'; # condition comes here
END LOOP;
close cur;
end;
Related
DELIMITER //
CREATE PROCEDURE PROCEDURE_NM ( IN PARAM_A VARCHAR(16)
, IN PARAM_B VARCHAR(16) )
BEGIN
DECLARE SRCH_CNTE VARCHAR(500) DEFAULT IN PARAM_A + PARAM_B;
DECLARE SQL_NM VARCHAR(500) DEFAULT 'PROCEDURE_NM';
DECLARE ERROR_YN VARCHAR(1) DEFAULT 'N';
DECLARE ERROR_MSG VARCHAR(500) DEFAULT NULL;
BEGIN
CALL PROC_LOG(SRCH_CNTE, SQL_NM, ERROR_YN, ERROR_MSG);
END;
BEGIN
--SOMETHING
END;
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
ERROR_MSG = MESSAGE_TEXT;
ERROR_YN = 'Y';
CALL PROC_LOG(SRCH_CNTE, SQL_NM, ERROR_YN, ERROR_MSG);
END;
END;
END//
DELIMITER ;
"ERROR_YN" is not valid at this position, expecting an identifier.
syntax error occurs.
I have completed the variable declaration, and I don't think there's anything missing.
What should I fix?
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 someVarient=RETURNED_SQLSTATE
, ERROR_MSG = MESSAGE_TEXT;
CALL PROC_LOG(SRCH_CNTE, SQL_NM, 'Y', ERROR_MSG);
END;
END;
It was successfully committed after changing to the code above.
similar to StackOverflowPosting I would like to calculate the Levenshtein Distance for a m x n matrix consisting of TITLE1 and TITLE2.
My Levenshtein Functions works fine and is from here: LD
But my Question is how can I loop through the m x n in a UDF?
The Result should be a table with m x n rows with LD, TITLE1 and TITLE2.
I have done this - BUT I ALWAYS GET AN ERROR
1338 Cursor Declaration after Handler Declaration
My UDF looks like this:
BEGIN
DECLARE bDone INT;
DECLARE bDone1 INT;
DECLARE var2 varCHAR(255); -- or approriate type
DECLARE Var1 INT;
DECLARE c1Var1 VARCHAR(250);
DECLARE curs CURSOR FOR SELECT recid as BIOTIrecid, replace(replace(BIOGRAPHYTITLE," [in SCOPUS]",""),"[SIMILAR]","") as bioti FROM BIO ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
DECLARE curs1 CURSOR FOR SELECT trim(concat(scopus.Titel," ",scopus.Untertitel)) as scopusti FROM scopus ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
DROP TABLE IF EXISTS LDResults;
CREATE TABLE `LDResults` (
`BIOGRAPHYTITLE` varchar(255) DEFAULT NULL,
`recid` int(11) NOT NULL AUTO_INCREMENT,
`BIOTIrecid` int(11) default NULL,
`LD` varchar(255) DEFAULT NULL,
`ScopusTI` varchar(255) DEFAULT NULL,
PRIMARY KEY (`recid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
OPEN curs1;
SET bDone1 = 0;
#---------------- run all rows for scopusti
REPEAT
FETCH curs1 into c1var1;
#-----------------------------------------
OPEN curs;
SET bDone = 0;
#----- run all COLUMNs for biographytitle
REPEAT
FETCH curs INTO var1, var2;
INSERT INTO LDResults (`BIOGRAPHYTITLE`, `BIOTIrecid`, `LD`, `ScopusTI`) VALUES (var2, var1, LEVENSHTEIN(var2,c1var1), c1var1);
UNTIL bDone END REPEAT;
#--------------------------------------------
CLOSE curs;
UNTIL bDone1 END REPEAT;
CLOSE curs1;
SELECT * FROM LDResults;
END
Is my way to solve this problem sophisticated or could this be done on a more faster and better solution ?
Thanks for all advices.
EDIT:
I could make it with a counter here: Any comments?
BEGIN
-- DECLARE bDone INT;
-- DECLARE bDone1 INT;
DECLARE i INT;
DECLARE var2 varCHAR(255); -- or approriate type
DECLARE Var1 INT;
DECLARE cVar1 VARCHAR(250);
DECLARE curs1 CURSOR FOR SELECT trim(concat(t.Titel," ",t.Untertitel)) as scopusti FROM tscopus t ;
DECLARE curs CURSOR FOR SELECT recid as BIOTIrecid, replace(replace(BIOGRAPHYTITLE," [in SCOPUS]",""),"[SIMILAR]","") as bioti FROM tBIO ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
#DECLARE curs1 CURSOR FOR SELECT trim(concat(t.Titel," ",t.Untertitel)) as scopusti FROM tscopus t ;
#DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
DROP TABLE IF EXISTS LDResults;
CREATE TABLE `LDResults` (
`BIOGRAPHYTITLE` varchar(255) DEFAULT NULL,
`recid` int(11) NOT NULL AUTO_INCREMENT,
`BIOTIrecid` int(11) default NULL,
`LD` varchar(255) DEFAULT NULL,
`ScopusTI` varchar(255) DEFAULT NULL,
PRIMARY KEY (`recid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
OPEN curs1;
SET i = 0;
SET bDone1 = 0;
-- ---------------- run all rows for scopusti
REPEAT
FETCH curs1 into cvar1;
set i=(i+1);
-- -----------------------------------------
OPEN curs;
SET bDone = 0;
-- ----- run all COLUMNs for biographytitle
REPEAT
FETCH curs INTO var1, var2;
INSERT INTO LDResults (`BIOGRAPHYTITLE`, `BIOTIrecid`, `LD`, `ScopusTI`) VALUES (var2, var1, LEVENSHTEIN(var2,cvar1), cvar1);
UNTIL bDone END REPEAT;
-- --------------------------------------------
CLOSE curs;
UNTIL (i >= 2) END REPEAT;
CLOSE curs1;
SELECT * FROM LDResults;
END
I mean you can do it by next way useng CROSS JOIN without loops in your code. CROSS JOIN by definition return product of two tables rows result.
So you can use this result and after some data manipulation insert the result into new table like:
DROP TABLE IF EXISTS LDResults;
CREATE TABLE `LDResults` (
`BIOGRAPHYTITLE` varchar(255) DEFAULT NULL,
`recid` int(11) NOT NULL AUTO_INCREMENT,
`BIOTIrecid` int(11) default NULL,
`LD` varchar(255) DEFAULT NULL,
`ScopusTI` varchar(255) DEFAULT NULL,
PRIMARY KEY (`recid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO LDResults (`BIOGRAPHYTITLE`, `BIOTIrecid`, `LD`, `ScopusTI`)
SELECT bioti, BIOTIrecid, LEVENSHTEIN(bioti,scopusti), scopusti
FROM (
SELECT
replace(replace(BIO.BIOGRAPHYTITLE," [in SCOPUS]",""),"[SIMILAR]","") as bioti,
BIO.recid as BIOTIrecid,
trim(concat(scopus.Titel," ",scopus.Untertitel)) as scopusti
FROM scopus
CROSS JOIN BIO
) tbl;
I am Trying to create stored procedure in MYSQLand getting below error.
I googled about it but no solution found please help me in this.
DELIMITER //
CREATE OR REPLACE PROCEDURE P_PROCESS_USER_STG ( OUT O_error_msg VARCHAR(3000),
OUT O_status VARCHAR(300),
IN I_uploaded_by INT (10))
BEGIN
declare L_program_name VARCHAR(100);
declare L_login_id INT(10) ;
declare L_password VARCHAR(100);
declare L_first_name VARCHAR(100);
declare L_last_name VARCHAR(100);
declare L_privilege_group_id INT(10) ;
declare L_group_id INT(10) ;
declare L_message VARCHAR(100);
declare L_date_of_upload TIMESTAMP ;
declare L_date_of_update TIMESTAMP ;
declare L_uploaded_by INT(10) ;
declare L_status VARCHAR(10) ;
declare L_error_msg VARCHAR(100),
declare L_finished INT(1) DEFAULT 0;
declare C_user_stg CURSOR FOR
SELECT LOGIN_ID,
PASSWORD,
FIRST_NAME,
LAST_NAME,
PRIVILEGE_GROUP_ID,
GROUP_ID
FROM uploaded_user_stg
where UPLOADED_BY = I_uploaded_by
and status in ( 'NEW' , 'UPDATE' );
declare CONTINUE HANDLER
FOR NOT FOUND SET L_finished = 1;
OPEN C_user_stg;
get_user: LOOP
FETCH C_user_stg INTO L_login_id ,
L_password ,
L_first_name ,
L_last_name ,
L_privilege_group_id,
L_group_id ;
IF L_finished = 1 THEN
LEAVE get_user;
END IF;
-- build email list
CALL P_CREATE_USER ( L_message ,
L_status ,
L_login_id ,
L_password ,
L_first_name,
L_last_name ,
L_privilege_group_id,
L_group_id )
UPDATE uploaded_user_stg
SET status = L_status,
error_msg = L_message
date_of_update = now();
where login_id = L_login_id;
END LOOP get_email;
CLOSE get_user;
END//
DELIMITER ;
I am getting Below error :
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that on for the right syntax to use near '
declare L_finished INT(1) DEFAULT 0;
declare C_' at line 18
Change this from:
declare L_error_msg VARCHAR(100),
to
declare L_error_msg VARCHAR(100);
I have following stored procedure that contains cursor.
But don't getting actual id field value and always show zero.
CREATE PROCEDURE `comm_pay_intro`(IN `id` VARCHAR(20), IN `comm_type` VARCHAR(50))
BEGIN
DECLARE i INTEGER;
DECLARE userid varchar(20);
DECLARE idx int(11);
DECLARE curs1 CURSOR FOR SELECT `id`, `user_id`
FROM `temp_table` order by `id`;
set i=1;
OPEN curs1;
read_loop: LOOP
FETCH curs1 INTO idx,userid;
insert into user_commission(rate_id, user_id)
VALUES(1, idx);
END LOOP read_loop;
CLOSE curs1;
END
I am trying to create a transaction procedure in MySQL and I keep receiving the following syntax error which is occurring in my variable declarations:
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 ';
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECL' at line 6
Below is my procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS new_employee$$
CREATE PROCEDURE new_employee(in_Fname VARCHAR(15),in_Lname VARCHAR(15),
in_hire DATE, in_DOB DATE, in_sal Numeric(7,2))
BEGIN
DECLARE out_message VARCHAR;
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECLARE out_empnum INT DEFAULT 0;
DECLARE time_created DATETIME;
DECLARE user_var VARCHAR;
DECLARE age_var Int DEFAULT 0;
START TRANSACTION;
Select MAX(Empno)
Into old_empnum
From emp;
Select COUNT(*)
Into emp_duplicate
From emp
Where (in_Fname=Fname AND in_Lname=Lname AND in_hire=HireDate AND in_DOB=DOB);
IF emp_duplicate=0 THEN
SET out_message = 'New employee update successful';
SET out_empnum = old_empnum + 1;
SET time_created = CURRENT_TIMESTAMP();
SET user_var = USER();
SET age_var = DATEDIFF(CURRENT_DATE,in_DOB);
INSERT INTO emp(Empno,Fname,Lname,HireDate,DOB,SAL)
VALUES(in_Fname,in_Lname,in_hire,in_DOB,in_sal);
INSERT INTO log(Empno,DateCreated,Who)
VALUES(out_empnum,time_created,user_var);
SELECT out_message;
SELECT Empno, Fname, Lname, DATEDIFF(CURRENT_DATE,DOB), HireDate
From emp;
ELSE
SET out_message = 'Employee already exists';
SELECT out_message;
END IF;
COMMIT;
END$$
DELIMITER ;
I am using cmd and notepad to execute my code (just in case that's helpful). For the life of me, I cannot see the error with my declarations?????
you need to define the VARCHAR size like this
DECLARE out_message VARCHAR(20);
try this
BEGIN
DECLARE out_message VARCHAR(20); // // varchar size here
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECLARE out_empnum INT DEFAULT 0;
DECLARE time_created DATETIME;
DECLARE user_var VARCHAR(20); // varchar size here
DECLARE age_var Int DEFAULT 0;
you forget to datatype size for DECLARE out_message VARCHAR (30) and DECLARE user_var VARCHAR(30);
DECLARE out_message VARCHAR (30); <------------- here
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECLARE out_empnum INT DEFAULT 0;
DECLARE time_created DATETIME;
DECLARE user_var VARCHAR(30); <------------- here
DECLARE age_var Int DEFAULT 0;
In insert statement you are missing value for Empno
INSERT INTO emp(Empno,Fname,Lname,HireDate,DOB,SAL)
VALUES(in_Fname,in_Lname,in_hire,in_DOB,in_sal);