Last active
December 29, 2015 12:49
-
-
Save davidfuhr/7673553 to your computer and use it in GitHub Desktop.
Finds zero datetime values in a mysql database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- -------------------------------------------------------------------------------- | |
-- Routine DDL | |
-- Note: comments before and after the routine body will not be stored by the server | |
-- -------------------------------------------------------------------------------- | |
DELIMITER $$ | |
CREATE PROCEDURE `select_zero_date_columns`() | |
BEGIN | |
DECLARE current_table_name CHAR(255); | |
DECLARE current_column_name CHAR(255); | |
DECLARE done INT DEFAULT FALSE; | |
DECLARE datetime_columns CURSOR FOR SELECT TABLE_NAME, COLUMN_NAME | |
FROM INFORMATION_SCHEMA.COLUMNS | |
WHERE TABLE_SCHEMA = SCHEMA() AND DATA_TYPE = "datetime"; | |
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; | |
DROP TABLE IF EXISTS tmp_output; | |
CREATE TEMPORARY TABLE tmp_output (`table_name` VARCHAR(255), `column_name` VARCHAR(255), `zero_count` INT UNSIGNED); | |
OPEN datetime_columns; | |
loop_columns: LOOP | |
FETCH datetime_columns INTO current_table_name, current_column_name; | |
IF done THEN | |
LEAVE loop_columns; | |
END IF; | |
SET @sql = CONCAT(' | |
INSERT INTO `tmp_output` | |
SELECT | |
"', current_table_name, '", | |
"', current_column_name, '", | |
COUNT(*) | |
FROM ', current_table_name, ' | |
WHERE ', current_column_name, ' = 0 | |
'); | |
PREPARE stmt FROM @sql; | |
EXECUTE stmt; | |
DROP PREPARE stmt; | |
END LOOP; | |
CLOSE datetime_columns; | |
SELECT * FROM tmp_output; | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment