I'm creating procedure called
min_max to find minimum and maximum for every column in a table.
But I'm encountering an error for declaring the variable. See below:
DELIMITER // DROP PROCEDURE IF EXISTS min_max // CREATE PROCEDURE min_max () BEGIN DECLARE col_name VARCHAR(64); DECLARE col_names CURSOR FOR SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'customer' ORDER BY ordinal_position; OPEN col_names; SET @num_rows=0; SET @i=0; SET @test=''; select FOUND_ROWS() into @num_rows; the_loop: LOOP IF @i > @num_rows THEN CLOSE col_names; LEAVE the_loop; END IF; FETCH col_names INTO col_name; SELECT CONCAT(MIN(col_name),"->",MAX(col_name)) as Min_Max_val FROM customer; SET @i = @i + 1; END LOOP the_loop; END // DELIMITER ; call min_max();
How should I define the variable correctly? What did I do wrong? I'm lost...
UPDATE: The result now:
last_update is one of the column names in