I need help in looping through columns and inserting values to columns.
My Obj:- Insert new value to selective columns in a table using stored-procedure i am using case statement to make selective choice and loop through to update list of columns
My code:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `loopcolnm1`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `loopcolnm1`()
BEGIN
DECLARE sColName VARCHAR(255);
DECLARE sEgStr VARCHAR(255);
DECLARE X INT;
SET segstr = ' ';
SET X = 0;
Loop_label : LOOP
IF X > 9 THEN LEAVE loop_label;
END IF;
SET X = X+1;
CASE
WHEN X= 1 THEN
SET sColName = 'age' ;
WHEN X= 2 THEN SET sColName = 'Gender' ;
WHEN X= 3 THEN SET sColName = 'incm_rng' ;
WHEN X= 4 THEN SET sColName = 'Prc_rng' ;
WHEN X= 5 THEN SET sColName = 'prop_purp';
WHEN X= 6 THEN SET sColName = 'time_rng' ;
WHEN X= 7 THEN SET sColName = 'time_rsn' ;
WHEN X= 8 THEN SET sColName = 'crrnt_dmcl' ;
WHEN X= 9 THEN SET sColName = 'Prop_Rsn' ;
ELSE SET sColName = '\'All\' as Sub_Crit';
END CASE ;
SET sEgStr = CONCAT(sEgstr,' ',X, ' ',',' ,sColName, ' '); -- test variable
INSERT INTO lptable (sColName)
VALUE ( CONCAT('loop + ',X));
END LOOP;
SELECT segstr AS cnct;
END$$
DELIMITER ;
alternatively i tried with a similar solution but, on both instance i get error
edit(1) - Additional Information
when i compile the stored procedure it does not throw up any error message however when i call the stored procedure
CALL loopcolnm1();
Error Code: 1054 Unknown column 'sColName' in 'field list'
i am new to mysql, i am not sure if this is something that i can do or i am erring in syntax, kindly help