By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE mytable2(id int, pos int, value int)
INSERT INTO mytable2 VALUES (1,1,1),(1,2,2),(1,3,3),(1,4,4),(1,5,5),(1,6,6)
Records: 6 Duplicates: 0 Warnings: 0
CREATE TABLE mytable(id int, pt1 int, pt2 int, pt3 int, pt4 int, pt5 int, pt6 int)
CREATE PROCEDURE `new_routine` (_id int, _sequence int, _pt1 int, _pt2 int,_pt3 int)
BEGIN
IF _sequence = 0 then
SET @sql := CONCAT('INSERT INTO mytable (id,pt1,pt2,pt3) VALUES (',_id,',',_pt1,',',_pt2,',',_pt3,')');
ELSE
SET @sql := CONCAT('UPDATE mytable SET pt',1 +((_sequence -1) * 3) ,'= ',_pt1
,', pt',2 +((_sequence -1) * 3) ,'= ',_pt2
,', pt',3 +((_sequence -1) * 3) ,'= ',_pt3,' WHERE id = ',_id);
END IF;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
CALL new_routine(1,0,1,1,1);
CALL new_routine(1,1,2,2,2)
CALL new_routine(1,2,3,3,3)
SELECT * FROM mytable
id | pt1 | pt2 | pt3 | pt4 | pt5 | pt6 |
---|---|---|---|---|---|---|
1 | 2 | 2 | 2 | 3 | 3 | 3 |
SELECT * FROM mytable2
id | pos | value |
---|---|---|
1 | 1 | 1 |
1 | 2 | 2 |
1 | 3 | 3 |
1 | 4 | 4 |
1 | 5 | 5 |
1 | 6 | 6 |