Dear, I have two tables, where table1 has columns from A1 to A4, B1 to B4, C1 to C4 and D1 to D4, all of type decimal (4.2) and with n rows; and table2 has 3 columns being Total1, Total2 and Total3, where the sum of the fields in table1 must be presented. Roughly I created the following procedure to update table2:
DELIMITER $$
DROP PROCEDURE IF EXISTS teste$$
CREATE PROCEDURE teste()
BEGIN
update tabela2 set TOTAL1 = (select sum(A1) from tabela1) where coluna = 'A1';
update tabela2 set TOTAL1 = (select sum(B1) from tabela1) where coluna = 'B1';
update tabela2 set TOTAL1 = (select sum(C1) from tabela1) where coluna = 'C1';
update tabela2 set TOTAL1 = (select sum(A2) from tabela1) where coluna = 'A2';
update tabela2 set TOTAL1 = (select sum(B2) from tabela1) where coluna = 'B2';
update tabela2 set TOTAL1 = (select sum(C2) from tabela1) where coluna = 'C2';
update tabela2 set TOTAL1 = (select sum(A3) from tabela1) where coluna = 'A3';
update tabela2 set TOTAL1 = (select sum(B3) from tabela1) where coluna = 'B3';
update tabela2 set TOTAL1 = (select sum(C3) from tabela1) where coluna = 'C3';
update tabela2 set TOTAL1 = (select sum(A4) from tabela1) where coluna = 'A4';
update tabela2 set TOTAL1 = (select sum(B4) from tabela1) where coluna = 'B4';
update tabela2 set TOTAL1 = (select sum(C4) from tabela1) where coluna = 'C4';
update tabela2 set TOTAL2 = (select sum(A1) from tabela1) where coluna = 'A1';
update tabela2 set TOTAL2 = (select sum(B1) from tabela1) where coluna = 'B1';
update tabela2 set TOTAL2 = (select sum(C1) from tabela1) where coluna = 'C1';
update tabela2 set TOTAL2 = (select sum(A2) from tabela1) where coluna = 'A2';
update tabela2 set TOTAL2 = (select sum(B2) from tabela1) where coluna = 'B2';
update tabela2 set TOTAL2 = (select sum(C2) from tabela1) where coluna = 'C2';
update tabela2 set TOTAL2 = (select sum(A3) from tabela1) where coluna = 'A3';
update tabela2 set TOTAL2 = (select sum(B3) from tabela1) where coluna = 'B3';
update tabela2 set TOTAL2 = (select sum(C3) from tabela1) where coluna = 'C3';
update tabela2 set TOTAL2 = (select sum(A4) from tabela1) where coluna = 'A4';
update tabela2 set TOTAL2 = (select sum(B4) from tabela1) where coluna = 'B4';
update tabela2 set TOTAL2 = (select sum(C4) from tabela1) where coluna = 'C4';
update tabela2 set TOTAL3 = (select sum(A1) from tabela1) where coluna = 'A1';
update tabela2 set TOTAL3 = (select sum(B1) from tabela1) where coluna = 'B1';
update tabela2 set TOTAL3 = (select sum(C1) from tabela1) where coluna = 'C1';
update tabela2 set TOTAL3 = (select sum(A2) from tabela1) where coluna = 'A2';
update tabela2 set TOTAL3 = (select sum(B2) from tabela1) where coluna = 'B2';
update tabela2 set TOTAL3 = (select sum(C2) from tabela1) where coluna = 'C2';
update tabela2 set TOTAL3 = (select sum(A3) from tabela1) where coluna = 'A3';
update tabela2 set TOTAL3 = (select sum(B3) from tabela1) where coluna = 'B3';
update tabela2 set TOTAL3 = (select sum(C3) from tabela1) where coluna = 'C3';
update tabela2 set TOTAL3 = (select sum(A4) from tabela1) where coluna = 'A4';
update tabela2 set TOTAL3 = (select sum(B4) from tabela1) where coluna = 'B4';
update tabela2 set TOTAL3 = (select sum(C4) from tabela1) where coluna = 'C4';
END$$
DELIMITER ;
all teste();
But this type of repetitive code is not legal, I'm trying to use the procedure below, but the sum does not work for the variable str, follow code:
DELIMITER $$
DROP PROCEDURE IF EXISTS teste$$
CREATE PROCEDURE teste()
BEGIN
DECLARE x INT;
DECLARE str1 varchar(2);
DECLARE str2 varchar(2);
DECLARE str3 varchar(2);
SET x = 1;
WHILE x <= 4 DO
SET str1 = concat('A',x);
SET str2 = concat('B',x);
SET str3 = concat('C',x);
update tabela set TOTAL = (select sum(str1) from tabela2) where coluna = str1;
update tabela set TOTAL = (select sum(str2) from tabela2) where coluna = str2;
update tabela set TOTAL = (select sum(str3) from tabela2) where coluna = str3;
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;
call teste();
I would like to know if there is a possibility to create this looping.
Thank you.