2

我们正在将我们的 mysql 数据库从非严格模式移动到 strict_all_tables,并希望从我们服务器上的所有数据库和表中找到所有具有无效日期 (0000-00-00) 的现有行。有没有办法通过一个命令或其他一些简单的方式来做到这一点?

我想知道存在 0000-00-00 值的数据库和表名,列名将是一个加号。

我们有相当多的数据库和大量的表,其中一些包含相当大量的数据,所以方法越自动化越好。

4

1 回答 1

3

试试这个http://code.google.com/p/anywhereindb/或这个http://forge.mysql.com/tools/tool.php?id=232

mikkom 编辑:这是复制的实际脚本,以防它有一天从外部源中消失:

#kedar.nitty-witty.com

## Table for storing resultant output
DROP TABLE IF EXISTS `temp_details`;
CREATE TABLE `temp_details` ( 
`t_schema` varchar(45) NOT NULL,
`t_table` varchar(45) NOT NULL,
`t_field` varchar(45) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



## Procedure for search in all fields of all databases
DELIMITER $$
#Script to loop through all tables using Information_Schema
DROP PROCEDURE IF EXISTS get_table $$
CREATE PROCEDURE get_table(in_search varchar(50))
READS SQL DATA
BEGIN
DECLARE trunc_cmd VARCHAR(50);
DECLARE search_string VARCHAR(250);
DECLARE db,tbl,clmn CHAR(50);
DECLARE done INT DEFAULT 0;
DECLARE COUNTER INT;
DECLARE table_cur CURSOR FOR
SELECT concat('SELECT COUNT(*) INTO @CNT_VALUE FROM `',table_schema,'`.`',table_name,'` WHERE `', column_name,'` REGEXP "',in_search,'"') ,table_schema,table_name,column_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA NOT IN ('information_schema','test','mysql');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
#Truncating table for refill the data for new search.
PREPARE trunc_cmd FROM "TRUNCATE TABLE temp_details;";
EXECUTE trunc_cmd ; 
OPEN table_cur;
table_loop:LOOP
FETCH table_cur INTO search_string,db,tbl,clmn;
#Executing the search
SET @search_string = search_string;
##SELECT  search_string;
PREPARE search_string FROM @search_string;
EXECUTE search_string;
SET COUNTER = @CNT_VALUE;
##SELECT COUNTER;
IF COUNTER>0 THEN
# Inserting required results from search to table
INSERT INTO temp_details VALUES(db,tbl,clmn);
END IF;
IF done=1 THEN
LEAVE table_loop;
END IF;
END LOOP;
CLOSE table_cur;
#Finally Show Results
SELECT * FROM temp_details;
END $$
DELIMITER ;
于 2012-01-02T09:47:27.637 回答