Question

I'm trying to create and use this procedure:

delimiter //
CREATE PROCEDURE doiterate(p1 INT)
BEGIN
DECLARE str VARCHAR(5000);
label1: LOOP

SET @query = 'SELECT * FROM ';

IF p1 > 0 THEN

SET @query = CONCAT(@query, ' wp_', pl, '_options,');
ITERATE label1;
END IF;
LEAVE label1;
END LOOP label1;

SET @query = substring(@query,1,length(@query)-1);

SET @query = CONCAT(@query, ' WHERE option_name = \'template\'OR option_name = \'stylesheet\' OR option_name = \'current_theme\'');

PREPARE stmt FROM @query;

EXECUTE stmt;

END//

and when I execute it I get this message:

Unknown column 'pl' in 'field list'

Can anyone point where did I do wrong?

Was it helpful?

Solution

You have a typo in your code:

SET @query = CONCAT(@query, ' wp_', pl, '_options,');

should be

SET @query = CONCAT(@query, ' wp_', p1, '_options,');

You have used "PL" (capitals to emphasize) instead of "p1";

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top