Update statement in stored procedure is not working - mysql

I have a stored procedure in Mysql database like this:
DELIMITER $$
USE `vboard_75`$$
DROP PROCEDURE IF EXISTS `sp_LongWaitCall`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_LongWaitCall`()
BEGIN
UPDATE cdr SET cdr.CallStatus='DISCONNECTED',cdr.EndTime=NOW() WHERE cdr.CallStatus='RINGINGIN'
AND MINUTE(DATEDIFF(cdr.StartTime,NOW())) >=7;
DECLARE _StatVal FLOAT;
DECLARE _DevID INT;
DECLARE Record_Fetch INT DEFAULT 0;
DECLARE crsr_Board CURSOR FOR
SELECT IFNULL(MAX(SECOND(DATEDIFF(CDR.StartTime, IFNULL(CDR.EndTime, NOW())))), 0) AS LRT,vw_Boards_Ext.boardid
FROM vw_Boards_Ext RIGHT OUTER JOIN
boards ON vw_Boards_Ext.boardid = boards.boardid LEFT OUTER JOIN
CDR ON vw_Boards_Ext.Ext = CDR.DDI AND
STR_TO_DATE(CONCAT(boards.ResetDate,' ',boards.ResetTime),'%m/%d/%Y %H:%i') < CDR.timestamp
AND CDR.CallStatus='RINGINGIN'
GROUP BY vw_Boards_Ext.boardid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET Record_Fetch = 1;
OPEN crsr_Board;
FETCH crsr_Board INTO _StatVal, _DevID;
WHILE Record_Fetch = 0 DO
UPDATE stat_values AS sv SET sv.StatValue = _StatVal,sv.timestamp= NOW()
WHERE sv.itemId = _DevID AND sv.itemType = 'boards' AND sv.StatId = 3;
FETCH crsr_Board INTO _StatVal, _DevID;
END WHILE;
CLOSE crsr_Board;
DEALLOCATE PREPARE crsr_Board;
END$$
DELIMITER ;
Error:
Query: CREATE DEFINER=root#localhost PROCEDURE sp_LongWaitCall() BEGIN update cdr set cdr.CallStatus='DISCONNECTED',cdr.EndTime=n...
Error Code: 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 _StatVal FLOAT;
DECLARE _DevID INT;
DECLARE Record_Fetch INT DEFAULT 0' at line 8
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.001 sec

This documentation refers to having the DECLARE statements at the top and also before the creation of any CURSORS and HANDLERS, which you are following but not at the beginning of the BEGIN... END block.
This documentation clarifies the question that OP had posted!
It is mandatory to have your declarations at the begin of your BEGIN... END block, but not under an UPDATE statement.
Additional reference to a similar issue can be found on this question, even which refers to the same solution.
Hope this helps!

Related

Declare inside if condition showing syntax error - MYSQL Triggers

I try to update some columns using trigger before insert
DROP TRIGGER IF EXISTS update_p_posts_places;
DELIMITER $$
CREATE TRIGGER update_p_posts_places BEFORE
INSERT
ON
`p_posts` FOR EACH ROW
BEGIN
DECLARE
p_post_group_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
BEGIN
DECLARE
place1_id_ int;
place2_id_ int;
place3_id_ int;
place4_id_ int;
place5_id_ int;
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
END $$
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
DELIMITER ;
It's showing some syntax errors.
#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
place1_id_ int;
place2_id_ int;
' at line 20
Here is a working trigger. I tested creating it on MySQL 5.7.
CREATE TRIGGER update_p_posts_places BEFORE INSERT ON `p_posts`
FOR EACH ROW
BEGIN
-- all declarations must be before any other statements
DECLARE p_post_group_id_, place1_id_, place2_id_, place3_id_,
place4_id_, place5_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
You can use one DECLARE for multiple local variables, but you must do like var1, var2, var3, ... int. In other words, name the type only once at the end. See documentation: https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
No need for the BEGIN..END inside the IF and definitely do not use $$ until after the last END because that will terminate the parser's interpretation of your whole CREATE TRIGGER statement before it's complete.

Nested Cursor Declare Issue Mysql

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.

how to declare & set cursor in MYSQL STORED PROCEDURE?

I am trying to use CURSOR inside mysql stored procedure... I am getting challenge while declaring the cursor... I error is **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 ';
SELECT count(*) INTO countitem from TBL_SHOPPING_CART_ITEM
SELECT Produ' at line 8
**
Please help me to solve this issue... Thanks in advance.. my code is like below,
delimiter //
CREATE PROCEDURE placeOrder(IN cartId INT)
BEGIN
DECLARE countitem INT;
DECLARE productId INT;
DECLARE cartId INT;
DECLARE itemDicountAmt INT;
DECLARE itemCursor CURSOR;
SET countitem = SELECT count(*) from TBL_SHOPPING_CART_ITEM
SET itemCursor = SELECT ProductId, Quantity FROM TBL_SHOPPING_CART_ITEM
OPEN itemCursor
WHILE countitem > 0
BEGIN
FETCH itemCursor into productId, cartId;
itemDicountAmt = calculateNetItemStandardDiscountAmount(productId, cartId);
insert into debugtable select concat('item discount amount', itemDicountAmt);
SET countitem = countitem - 1;
END
CLOSE itemCursor
DEALLOCATE itemCursor
END//
delimiter ;
Don't worry, this isn't a bug.
The DECLARE clause is used to tell your machine that a local variable exists. The operations are made after the declarations, by aknowledging the existing, declared local variables.
So in your case, you tried to add a new declaration of variable after the machine started calculating, which isn't possible in MySQL. You'll have to find another way to use your last variable.
To assign a new content to a variable, first use DECLARE to create your variable first, then use SET as an assignment to the declared variable. I'll leave this link right here so you can get to know how to use it.

MySQL: Solve my MySQL Cursor Error:1064

DROP TRIGGER IF EXISTS `ACTUALIZAR_LOTE_DETALLE`;
DELIMITER //
CREATE TRIGGER `ACTUALIZAR_LOTE_DETALLE` AFTER INSERT ON `detalle_envio`
FOR EACH ROW BEGIN
DECLARE ID_INVENTARIO_IN INT;
DECLARE ID_LOTE_DETALLE_MIN INT;
DECLARE CANTIDAD_A_COMPARAR INT;
DECLARE CANTIDAD_A_RESTAR INT;
SET CANTIDAD_A_RESTAR = new.cantidad_enviado;
SET ID_INVENTARIO_IN = (Select id_inventario from detalle_requisicion WHERE id_detalle_requisicion = new.id_detalle_requisicion);
DECLARE cur_id CURSOR FOR SELECT id_lote_detalle from lote_detalle WHERE id_inventario = ID_INVENTARIO_IN AND cantidad > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = 1;
OPEN cur_id;
read_loop: LOOP
FETCH cur_id INTO LOTE_DETALLE_IDS;
SET CANTIDAD_A_COMPARAR = select cantidad from lote_detalle where id_lote_detalle = LOTE_DETALLE_IDS;
IF CANTIDAD_A_RESTAR > CANTIDAD_A_COMPARAR THEN
UPDATE `lote_detalle` SET cantidad=cantidad-CANTIDAD_A_COMPARAR WHERE id_lote_detalle = LOTE_DETALLE_IDS;
SET CANTIDAD_A_RESTAR = CANTIDAD_A_RESTAR - CANTIDAD_A_COMPARAR;
ELSE
UPDATE `lote_detalle` SET cantidad=cantidad-CANTIDAD_A_RESTAR WHERE id_lote_detalle = LOTE_DETALLE_IDS;
LEAVE read_loop;
END IF;
IF DONE = 1 THEN
LEAVE read_loop;
END IF;
END LOOP read_loop;
CLOSE cur_id;
END
//
DELIMITER ;
Is anything in the cursor?
the query or something?
Is anything in the Syntax?
Cause i have based this Trigger from the syntax of the MySQL Forums
This is 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 'DECLARE cur_id CURSOR FOR SELECT id_lote_detalle from lote_detalle WHERE id_inve' at line 9
There are at least following issues with your code:
All declarations should come before any statements (SET in your case). That's what is causing your immediate error.
You didn't declare DONE variable
You didn't declare LOTE_DETALLE_IDS variable, but you declared ID_LOTE_DETALLE_MIN which you didn't use. Perhaps you should rename ID_LOTE_DETALLE_MIN to LOTE_DETALLE_IDS then.
Since declarations go first you have to re-write your cursor definition (e.g. with subquery) because ID_INVENTARIO_IN variable isn't initialized yet
Your whole declaration section can be re-written in a following manner
-- declaration of variables of the same type can be combined
DECLARE ID_INVENTARIO_IN,
LOTE_DETALLE_IDS,
CANTIDAD_A_COMPARARINT,
CANTIDAD_A_RESTAR,
DONE INT;
DECLARE cur_id CURSOR FOR ...
DECLARE CONTINUE HANDLER ...
-- initialize variables
SET ...
Now, I believe your trigger most likely can be re-written with just update statement(s). You can get help with it if you explain in plain words what you're trying to achieve and provide exact table schemas and sample data.

syntax error on DECLARE CURSOR FOR

I dont't understand why im getting syntax error on my sp code below. Can anyone help me figure this out?
SQL 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 'DECLARE CUR1 CURSOR FOR SELECT pc.prospectus_courses_id FROM
prereq_cou' at line 8
DELIMITER $$
DROP PROCEDURE IF EXISTS get_prereqs3$$
CREATE PROCEDURE get_prereqs3(IN prosp_courses_id SMALLINT(5))
BEGIN
DECLARE done int DEFAULT FALSE;
DECLARE required SMALLINT(5) default 0;
DECLARE to_search SMALLINT(5) default 0;
DROP TABLE IF EXISTS tmp_list;
CREATE TABLE tmp_list(courses_id SMALLINT(5), courses_id_req SMALLINT(5)) ENGINE = MEMORY;
DECLARE CUR1 CURSOR FOR SELECT pc.prospectus_courses_id
FROM prereq_courses pc
JOIN prerequisites pr on (pr.id = pc.prerequisites_id)
JOIN prospectus_courses ps on (ps.id = pr.prospectus_courses_id)
WHERE ps.id = to_search
MAIN_LOOP: LOOP
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
FETCH cur1 INTO required;
IF done THEN
CLOSE cur1;
LEAVE main_loop;
ELSE
insert into tmp_list values (to_search, required);
set to_search = required;
iterate main_loop;
END IF;
END LOOP;
select c.course_code
from tmp_list t
join prospectus_courses pc on pc.id = t.courses_id_req
join courses c on c.id = pc.courses_id ;
drop table tmp_list;
END$$
DELIMITER ;
Declarations have to be right after a BEGIN block.
In your case just move the DECLARE cur1 CURSOR and DECLARE CONTINUE HANDLER.. two lines up.
Sometimes you want to declare a variable or cursor later in the code, for example only, if a condition is met.
In this case you can wrap the block with a nested BEGIN .. END again.
http://dev.mysql.com/doc/refman/5.5/en/begin-end.html and
http://dev.mysql.com/doc/refman/5.5/en/declare.html
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
Also you are declaring CUR1 but using cur1.
Is there no need semicolon?
WHERE ps.id = to_search;
^___________