Home > database > Pray god help me to write a SQL or stored procedures: query out all of the field is not used in the
Pray god help me to write a SQL or stored procedures: query out all of the field is not used in the
Time:10-06
The mysql database: Such as a table: sr_problem_process, This table has two hundred fields, Some fields not used (i.e., all values of this field is null), Query out all the fields, Using SQL or stored procedures, Pray god to help write, thanks
CodePudding user response:
To information_schema. COLUMNS table with concat joining together a bunch of SQL statement to execute
CodePudding user response:
DROP PROCEDURE IF the EXISTS find_null_col;
CREATE PROCEDURE find_null_col ( IN tableName VARCHAR (20), IN dbName VARCHAR (20), OUT the result VARCHAR (200) ) - create a stored procedure named tests The BEGIN
DECLARE col_name VARCHAR (50);
DECLARE no_more_record TINYINT DEFAULT 0;
DECLARE rs_cursor CURSOR FOR the SELECT COLUMN_NAME The FROM INFORMATION_SCHEMA. COLUMNS WHERE Table_name=@ tableName AND table_schema=@ dbName;
DECLARE the CONTINUE HANDLER FOR the NOT FOUND The SET no_more_record=1;
The OPEN rs_cursor;
The SET @ tableName=CONCAT (tableName);
The SET @ dbName=CONCAT (dbName);
REPEAT The FETCH rs_cursor INTO col_name;
IF NOT no_more_record THEN
The SET @ sqlStr=CONCAT ( 'the select 1 from' @ tableName, 'where', Col_name, 'is not null limit 1 );
PREPARE STMT The FROM @ sqlStr;
- predefined a statement and to assign it to STMT The EXECUTE STMT.
IF FOUND_ROWS ()=0 THEN SELECT CONCAT_WS (', ', the result, col_name) INTO the result.
END IF;
- execute statement DEALLOCATE PREPARE STMT.
A prepared statement - to release resources END IF;