I'm trying to implement a nested loop in Mysql without cursors.
DELIMITER $$
CREATE FUNCTION abc()
RETURNS INT
BEGIN DECLARE val INT;
DECLARE row,col INT DEFAULT 1;
SET val=1;
row_loop: LOOP
SET col=1;
col_loop: LOOP
SET col=col+1;
SET val=val+1;
IF col>8 THEN
LEAVE col_loop;
END IF;
END LOOP col_loop;
SET row=row+1;
IF row>8 THEN
LEAVE row_loop;
END IF;
RETURN val;
END LOOP row_loop;
END;
$$
The output should be 64 rather but I'm getting 8 as the inner loop is not running after 1st iteration.
Your logic is fine, except the return statement is inside the loop:
row_loop: LOOP
SET col=1;
col_loop: LOOP
SET col=col+1;
SET val=val+1;
IF col>8 THEN
LEAVE col_loop;
END IF;
END LOOP col_loop;
SET row=row+1;
IF row>8 THEN
LEAVE row_loop;
END IF;
END LOOP row_loop;
RETURN val;
Related
Variable captiono is not dynamic in BLOCK3. It is constant in BLOCK3 though is suppose to change while iterator2 loop.
BLOCK2: BEGIN
DECLARE cur2 CURSOR FOR SELECT id, caption FROM
mazhorik.catalog_items_content;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;
OPEN cur2;
iterator2: LOOP
IF done2 THEN
LEAVE iterator2;
END IF;
FETCH cur2 INTO ido, captiono;
BLOCK3: BEGIN
iterator3 : LOOP
SET idoo = ido;
SET captionoo = captiono;
SET captionoo = REPLACE(captionoo, element, '');
SET camel = CONCAT(UCASE(LEFT(element, 1)),
LCASE(SUBSTRING(element, 2)));
END LOOP iterator3;
END BLOCK3;
END LOOP iterator2;
CLOSE cur2;
END BLOCK2;
Unable to use for each loop in trigger
I am getting
Error Code: 1064
DELIMITER $$
CREATE
TRIGGER `TRG_AU_DEVICES_HOWLONG` AFTER UPDATE ON `devices`
FOR EACH ROW BEGIN
DECLARE lastid INTEGER;
DECLARE a, b, c VARCHAR(255);
SET #lastid := (SELECT deviceId FROM devices ORDER BY packetDate DESC LIMIT 1);
DECLARE cur1 CURSOR FOR SELECT alertType,deviceId FROM alerts WHERE alerts.deviceId = #lastid ;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a, b;
insert into test(alertType,deviceId) values(a,b);
END LOOP;
CLOSE cur1;
END;
$$
DELIMITER ;
Unable to use for each loop in trigger, am getting Error Code: 1064.How to use for each loop in trigger
DELIMITER $$
CREATE
TRIGGER TRG_TEST BEFORE UPDATE ON devices
FOR EACH ROW BEGIN
DECLARE done INT DEFAULT 0;
DECLARE alert VARCHAR(255);
DECLARE my_cur CURSOR FOR SELECT alertType FROM alerts WHERE deviceId = 8170610948;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN my_cur;
my_cur_loop:
LOOP FETCH my_cur INTO a;
IF done = 1 THEN
LEAVE my_cur_loop;
END IF;
INSERT INTO test (alertType)VALUES(alert);
END LOOP my_cur_loop;
CLOSE my_cur;
END;
$$
You are referencing your own looped array inside the array. I call this a loop bomb.
Since you expect the statement inside the loop to return control to the loop, but the flow of logic can not reversed halfway through a loop mechanically.It can be conditioned, not reversed because the array is not expressed, it is defined.
To Achieve logical expression of the array before the construct, consider using, predefined data matrices, lambda functions or Using Lambda programming that includes meta or reflective paradigms. Lisp, GUILE, OCAMEL are examples.
im trying to update a table using this query:
DELIMITER $$
CREATE PROCEDURE updateDataSetHasChildren()
BEGIN
DECLARE data_set_id INT;
DECLARE done INT DEFAULT FALSE;
DECLARE result INT DEFAULT FALSE;
DECLARE data_set_cursor CURSOR FOR
SELECT id_data_set FROM data_set;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN data_set_cursor;
myloop: LOOP
-- Perform the first fetch.
FETCH data_set_cursor into data_set_id;
IF done THEN
LEAVE myloop;
END IF;
IF (SELECT COUNT(*) FROM data_sub_set WHERE id_data_set = data_set_id)>0 THEN
UPDATE data_set
SET has_children = TRUE
WHERE id_data_set = data_set_id;
ELSE
UPDATE data_set
SET has_children = FALSE
WHERE id_data_set = data_set_id;
END IF;
FETCH data_set_cursor into data_set_id;
END LOOP myloop;
CLOSE data_set_cursor;
END$$
DELIMITER ;
The problem is that when i run it it fills the column of the table with 1 0 1 0 1 0...and when i check the values are incorrect, what am i missing here?
Regards,
Remove second
FETCH data_set_cursor into data_set_id;
at the end of the myloop. Now it is working by steps:
Fetch id.
Process row.
Fetch id (loop end).
Fetch id (second loop iteration started).
Process row.
Fetch id.
Fetch id and so on...
Fetch is performed twice, and you are really processing every second row.
I am using below but getting error, what is wrong in this code :
declare
begin
for i in 1..20
loop
execute immediate 'update table IMP_BACKUP set name='XYZ' where status='INVALID'';
end loop;
end;
/
Declare variable i as
declare
i number(2);
begin
for i in 1..20 loop
execute immediate 'update table IMP_BACKUP set name=''XYZ'' where status=''INVALID''';
end loop;
end;
/
I have a syntax error with my code, which adds 1000 random records to a table.\
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
first syntax error is at default 1 saying it's missing a semicolon, then at my_loop and there are like 4 more.. Any help please? It seems good to go from what I've gooogled.
You need to change the delimiter before defining the statement:
DELIMITER $$
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
$$
Otherwise, the ; will end the entire CREATE PROCEDURE statement.