查詢數據庫中有多少個表:
SELECT COUNT(*) TABLES, table_schema FROM information_schema.TABLES WHERE table_schema ='soc_common' GROUP BY table_schema;
以下代碼親測:(比較兩個數據庫中不同)
-- 1.將mysql分隔符從;設置為&
DELIMITER &
-- 2.如果存在存儲過程getdatabaseCount則刪除
DROP PROCEDURE IF EXISTS `getdatabaseCount` &
-- 3.定義存儲過程,獲取特定數據庫的數量
-- (傳入參數database_name字符串類型,為數據庫名;傳出參數count_date整數類型,為數量)
CREATE DEFINER=`oms`@`localhost` PROCEDURE getdatabaseCount(IN database_name CHAR(20), OUT count_date INT)
BEGIN
-- 4.聲明變量
DECLARE $sqltext VARCHAR(1000);
-- 5.動態sql,把sql返回值放到@count_date中
SET $sqltext = CONCAT('SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\';');
SET @sqlcounts := $sqltext;
-- 6.預編釋,stmt預編釋變量的名稱
PREPARE stmt FROM @sqlcounts;
-- 7.執行SQL語句
EXECUTE stmt;
-- 8.釋放資源
DEALLOCATE PREPARE stmt;
-- 9.獲取動態SQL語句返回值
SET count_date = @count_date;
END
-- 10.定義存儲過程結束
&
-- 11.如果存在存儲過程getTableCount則刪除
DROP PROCEDURE IF EXISTS `getTableCount` &
-- 12.定義存儲過程,獲取特定數據庫表的數量
-- (傳入參數database_name字符串類型,為數據庫名;傳入參數table_name字符串類型,為表名;傳出參數count_date整數類型,為數量)
CREATE DEFINER=`oms`@`localhost` PROCEDURE getTableCount(IN database_name CHAR(20), IN table_name CHAR(200), OUT count_date INT)
BEGIN
-- 13.聲明變量
DECLARE $sqltext VARCHAR(1000);
-- 14.動態sql,把sql返回值放到@count_date中
SET $sqltext = CONCAT('SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\' and t.`TABLE_NAME` = \'', table_name, '\';');
SET @sqlcounts := $sqltext;
-- 15.預編釋,stmt預編釋變量的名稱
PREPARE stmt FROM @sqlcounts;
-- 16.執行SQL語句
EXECUTE stmt;
-- 17.釋放資源
DEALLOCATE PREPARE stmt;
-- 18.獲取動態SQL語句返回值
SET count_date = @count_date;
END
-- 19.定義存儲過程結束
&
-- 20.如果存在存儲過程getColumnCount則刪除
DROP PROCEDURE IF EXISTS `getColumnCount` &
-- 21.定義存儲過程,獲取特定數據庫表列的數量
-- (傳入參數database_name字符串類型,為數據庫名;傳入參數table_name字符串類型,為表名;傳入參數column_name字符串類型,為列名;傳出參數count_date整數類型,為數量)
CREATE DEFINER=`oms`@`localhost` PROCEDURE getColumnCount(IN database_name CHAR(20), IN table_name CHAR(200), IN column_name CHAR(200), OUT count_date INT)
BEGIN
-- 22.聲明變量
DECLARE $sqltext VARCHAR(1000);
-- 23.動態sql,把sql返回值放到@count_date中
SET $sqltext = CONCAT('SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\' and t.`TABLE_NAME` = \'', table_name, '\' and t.`COLUMN_NAME` = \'', column_name, '\';');
SET @sqlcounts := $sqltext;
-- 24.預編釋,stmt預編釋變量的名稱
PREPARE stmt FROM @sqlcounts;
-- 25.執行SQL語句
EXECUTE stmt;
-- 26.釋放資源
DEALLOCATE PREPARE stmt;
-- 27.獲取動態SQL語句返回值
SET count_date = @count_date;
END
-- 28.定義存儲過程結束
&
-- 29.如果存在存儲過程getColumnInfo則刪除
DROP PROCEDURE IF EXISTS `getColumnInfo` &
-- 30.定義存儲過程,獲取特定數據庫表列的信息
-- (傳入參數database_name字符串類型,為數據庫名;傳入參數table_name字符串類型,為表名;傳入參數column_name字符串類型,為列名;傳入參數column_info字符串類型,列信息;傳出參數result_data字符串類型,信息)
CREATE DEFINER=`oms`@`localhost` PROCEDURE getColumnInfo(IN database_name CHAR(20), IN table_name CHAR(200), IN column_name CHAR(200), IN column_info CHAR(50), OUT result_data CHAR(20))
BEGIN
-- 31.聲明變量
DECLARE $sqltext VARCHAR(1000);
-- 32.動態sql,把sql返回值放到@count_date中
SET $sqltext = CONCAT('SELECT t.', column_info,' into @column_info FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\' and t.`TABLE_NAME` = \'', table_name, '\' and t.`COLUMN_NAME` = \'', column_name, '\';');
SET @sqlcounts := $sqltext;
-- 33.預編釋,stmt預編釋變量的名稱
PREPARE stmt FROM @sqlcounts;
-- 34.執行SQL語句
EXECUTE stmt;
-- 35.釋放資源
DEALLOCATE PREPARE stmt;
-- 36.獲取動態SQL語句返回值
SET result_data = @column_info;
END
-- 37.定義存儲過程結束
&
-- 38.如果存在存儲過程comparisonTableExist則刪除
DROP PROCEDURE IF EXISTS `comparisonTableExist` &
-- 39.定義存儲過程,對比表是否存在
-- (傳入參數database_1字符串類型,數據庫名1;傳入參數database_2字符串類型,數據庫名2;傳入參數info字符串類型,庫表信息)
CREATE DEFINER=`oms`@`localhost` PROCEDURE comparisonTableExist(IN database_1 CHAR(20), IN database_2 CHAR(20), OUT info MEDIUMTEXT)
BEGIN
-- 40.聲明變量。database_name查詢出來的數據庫,table_name查詢出來的表名
DECLARE database_name, table_name CHAR(200);
-- this_info表不存在的記錄,database_table_no表不存在的記錄跳過重復查詢
DECLARE this_info, database_table_no MEDIUMTEXT DEFAULT '';
-- database_count_1統計數據庫1存在的數量,database_count_2統計數據庫2存在的數量,resoult_count統計表存在的數量。如果為0表示不存在
DECLARE database_count_1, database_count_2, resoult_count INT DEFAULT 0;
-- 41.定義游標結束標識,默認為0
DECLARE stopflag INT DEFAULT 0;
-- 42.定義游標,其實就是臨時存儲sql返回的集合
DECLARE sql_resoult CURSOR FOR SELECT t.TABLE_SCHEMA, t.TABLE_NAME FROM information_schema.COLUMNS t;
-- 43.游標結束就設置為1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag=1;
-- 44.調用存儲過程getdatabaseCount,查看兩個數據庫是否存在,都存在則繼續
CALL getdatabaseCount(database_1, database_count_1);
CALL getdatabaseCount(database_2, database_count_2);
IF (database_count_1 <> 0 AND database_count_2 <> 0) THEN
-- 45.打開游標
OPEN sql_resoult;
-- 46.讀取游標中數據,存儲到指定變量
FETCH sql_resoult INTO database_name, table_name;
-- 47.沒有結束繼續往下走
WHILE (stopflag=0) DO
BEGIN
-- 48.判斷數據庫是否為輸入的數據庫名稱1,去除已經比較過數據庫2的表不存在
IF (database_name=database_1 AND INSTR(database_table_no, CONCAT(database_2, '_', table_name)) = 0) THEN
-- 49.調用存儲過程getTableCount,查看表是否存在
CALL getTableCount(database_2, table_name, resoult_count);
-- 50.如果數量等於0,那么表不存在
IF (resoult_count = 0) THEN
-- 51.把不存在的表記錄下來
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_2, '的', table_name, '表不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_2, '的', table_name, '表不存在;\n');
END IF;
SET database_table_no=CONCAT(database_table_no, ';', database_2, '_', table_name, ';');
END IF;
ELSE
-- 52.判斷數據庫是否為輸入的數據庫名稱2,去除已經比較過數據庫1的表不存在
IF (database_name=database_2 AND INSTR(database_table_no, CONCAT(database_1, '_', table_name)) = 0) THEN
CALL getTableCount(database_1, table_name, resoult_count);
IF (resoult_count = 0) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_1, '的', table_name, '表不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_1, '的', table_name, '表不存在;\n');
END IF;
SET database_table_no=CONCAT(database_table_no, ';', database_1, '_', table_name, ';');
END IF;
END IF;
END IF;
-- 53.讀取游標中數據,存儲到指定變量。(和46一樣)
FETCH sql_resoult INTO database_name, table_name;
END;
END WHILE;
-- 54.關閉游標
CLOSE sql_resoult;
ELSE
IF (database_count_1 = 0 AND database_count_2 = 0) THEN
SET this_info = CONCAT(database_1, '和', database_2, '數據庫不存在或為空數據庫');
ELSE
IF (database_count_1 = 0) THEN
SET this_info = CONCAT(database_1, '數據庫不存在或為空數據庫');
ELSE
SET this_info = CONCAT(database_2, '數據庫不存在或為空數據庫');
END IF;
END IF;
END IF;
-- 55.把數據放到傳出參數
SET info=this_info;
END
-- 56.定義存儲過程結束
&
-- 57.如果存在存儲過程comparisonColumnExist則刪除
DROP PROCEDURE IF EXISTS `comparisonColumnExist` &
-- 58.定義存儲過程,對比列是否存在
-- (傳入參數database_1字符串類型,數據庫名1;傳入參數database_2字符串類型,數據庫名2;傳入參數info字符串類型,庫表信息)
CREATE DEFINER=`oms`@`localhost` PROCEDURE comparisonColumnExist(IN database_1 CHAR(20), IN database_2 CHAR(20), OUT info MEDIUMTEXT)
BEGIN
-- 59.聲明變量。database_name查詢出來的數據庫,table_name查詢出來的表名,column_name查詢出來的列名
DECLARE database_name, table_name, column_name CHAR(200);
-- this_info表不存在的記錄,database_table_no表不存在的記錄跳過重復查詢
DECLARE this_info, database_table_no TEXT DEFAULT '';
-- database_count_1統計數據庫1存在的數量,database_count_2統計數據庫2存在的數量,resoult_count統計表存在的數量。如果為0表示不存在
DECLARE database_count_1, database_count_2, resoult_count INT DEFAULT 0;
-- 60.定義游標結束標識,默認為0
DECLARE stopflag INT DEFAULT 0;
-- 61.定義游標,其實就是臨時存儲sql返回的集合
DECLARE sql_resoult CURSOR FOR SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.COLUMN_NAME FROM information_schema.COLUMNS t;
-- 62.游標結束就設置為1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag=1;
-- 63.調用存儲過程getdatabaseCount,查看兩個數據庫是否存在,都存在則繼續(同44)
CALL getdatabaseCount(database_1, database_count_1);
CALL getdatabaseCount(database_2, database_count_2);
IF (database_count_1 <> 0 AND database_count_2 <> 0) THEN
-- 64.打開游標
OPEN sql_resoult;
-- 65.讀取游標中數據,存儲到指定變量
FETCH sql_resoult INTO database_name, table_name, column_name;
-- 66.沒有結束繼續往下走
WHILE (stopflag=0) DO
BEGIN
-- 67.判斷數據庫是否為輸入的數據庫名稱1,去除已經比較過數據庫2的表不存在(同48)
IF (database_name=database_1 AND INSTR(database_table_no, CONCAT(database_2, '_', table_name)) = 0) THEN
-- 68.調用存儲過程getTableCount,查看表是否存在(同49)
CALL getTableCount(database_2, table_name, resoult_count);
-- 69.如果數量不等於0,則繼續
IF (resoult_count <> 0) THEN
-- 70.調用存儲過程getColumnCount,查看列是否存在。為0說明不存在
CALL getColumnCount(database_2, table_name, column_name, resoult_count);
IF (resoult_count = 0) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_2, '的', table_name, '表的', column_name, '列不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_2, '的', table_name, '表的', column_name, '列不存在;\n');
END IF;
END IF;
ELSE
SET database_table_no=CONCAT(database_table_no, ';', database_2, '_', table_name, ';');
END IF;
ELSE
-- 71.判斷數據庫是否為輸入的數據庫名稱2,去除已經比較過數據庫1的表不存在(同52)
IF (database_name=database_2 AND INSTR(database_table_no, CONCAT(database_1, '_', table_name)) = 0) THEN
CALL getTableCount(database_1, table_name, resoult_count);
IF (resoult_count <> 0) THEN
CALL getColumnCount(database_1, table_name, column_name, resoult_count);
IF (resoult_count = 0) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_1, '的', table_name, '表的', column_name, '列不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_1, '的', table_name, '表的', column_name, '列不存在;\n');
END IF;
END IF;
ELSE
SET database_table_no=CONCAT(database_table_no, ';', database_1, '_', table_name, ';');
END IF;
END IF;
END IF;
-- 72.讀取游標中數據,存儲到指定變量。(和65一樣)
FETCH sql_resoult INTO database_name, table_name, column_name;
END;
END WHILE;
-- 73.關閉游標
CLOSE sql_resoult;
END IF;
-- 74.把數據放到傳出參數
SET info=this_info;
END
-- 75.定義存儲過程結束
&
-- 76.如果存在存儲過程comparisonColumnInfo則刪除
DROP PROCEDURE IF EXISTS `comparisonColumnInfo` &
-- 77.定義存儲過程,對比列的不同
-- (傳入參數database_1字符串類型,數據庫名1;傳入參數database_2字符串類型,數據庫名2;傳入參數info字符串類型,庫表信息)
CREATE DEFINER=`oms`@`localhost` PROCEDURE comparisonColumnInfo(IN database_1 CHAR(20), IN database_2 CHAR(20),OUT info MEDIUMTEXT)
BEGIN
-- 78.聲明變量。database_name查詢出來的數據庫,table_name查詢出來的表名,column_name查詢出來的列名,collation_name查詢出來的具體編碼類型
-- result_data_1數據庫1的列信息,result_data_2數據庫2的列信息,column_info對比的列(現在只比較DATA_TYPE、CHARACTER_SET_NAME)
DECLARE database_name, table_name, column_name, collation_name, result_data_1, result_data_2, column_info CHAR(200);
-- this_info表不存在的記錄,database_table_no表不存在的記錄跳過重復查詢
DECLARE this_info, database_table_no MEDIUMTEXT DEFAULT '';
-- database_count_1統計數據庫1存在的數量,database_count_2統計數據庫2存在的數量,resoult_count統計表存在的數量。如果為0表示不存在
DECLARE database_count_1, database_count_2, resoult_count INT DEFAULT 0;
-- 79.定義游標結束標識,默認為0
DECLARE stopflag INT DEFAULT 0;
-- 80.定義游標,其實就是臨時存儲sql返回的集合
DECLARE sql_resoult CURSOR FOR SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.COLUMN_NAME, t.COLLATION_NAME FROM information_schema.COLUMNS t;
-- 81.游標結束就設置為1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag=1;
-- 82.調用存儲過程getdatabaseCount,查看兩個數據庫是否存在,都存在則繼續(同63)
CALL getdatabaseCount(database_1, database_count_1);
CALL getdatabaseCount(database_2, database_count_2);
IF (database_count_1 <> 0 AND database_count_2 <> 0) THEN
-- 83.打開游標
OPEN sql_resoult;
-- 84.讀取游標中數據,存儲到指定變量
FETCH sql_resoult INTO database_name, table_name, column_name, collation_name;
-- 85.沒有結束繼續往下走
WHILE (stopflag=0) DO
BEGIN
-- 86.判斷數據庫是否為輸入的數據庫名稱1,去除已經比較過數據庫2的表不存在(同67)
IF (database_name=database_1 AND INSTR(database_table_no, CONCAT(database_2, '_', table_name)) = 0) THEN
-- 87.調用存儲過程getTableCount,查看表是否存在(同68)
CALL getTableCount(database_2, table_name, resoult_count);
-- 88.如果數量不等於0,則繼續
IF (resoult_count <> 0) THEN
-- 89.調用存儲過程getColumnCount,查看列是否存在。為0說明不存在(同70)
CALL getColumnCount(database_2, table_name, column_name, resoult_count);
IF (resoult_count <> 0) THEN
-- 90.對比DATA_TYPE是否相同
SET column_info = 'DATA_TYPE';
CALL getColumnInfo(database_1, table_name, column_name, column_info, result_data_1);
CALL getColumnInfo(database_2, table_name, column_name, column_info, result_data_2);
IF (result_data_1 <> result_data_2) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
ELSE
SET this_info=CONCAT(this_info, table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
END IF;
END IF;
-- 91.對比CHARACTER_SET_NAME是否相同
SET column_info = 'CHARACTER_SET_NAME';
CALL getColumnInfo(database_1, table_name, column_name, column_info, result_data_1);
CALL getColumnInfo(database_2, table_name, column_name, column_info, result_data_2);
IF (result_data_1 <> result_data_2) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
ELSE
SET this_info=CONCAT(this_info, table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
END IF;
END IF;
END IF;
ELSE
SET database_table_no=CONCAT(database_table_no, ';', database_2, '_', table_name, ';');
END IF;
ELSE
IF (database_name=database_2 AND INSTR(database_table_no, CONCAT(database_1, '_', table_name)) = 0) THEN
CALL getTableCount(database_1, table_name, resoult_count);
IF (resoult_count = 0) THEN
SET database_table_no=CONCAT(database_table_no, ';', database_1, '_', table_name, ';');
END IF;
END IF;
END IF;
-- 92.讀取游標中數據,存儲到指定變量。(和84一樣)
FETCH sql_resoult INTO database_name, table_name, column_name, collation_name;
END;
END WHILE;
-- 93.關閉游標
CLOSE sql_resoult;
END IF;
-- 94.把數據放到傳出參數
SET info=this_info;
END
-- 95.定義存儲過程結束
&
-- 96.將mysql分隔符從&設置為;
DELIMITER ;
-- 97.設置變量
SET @database_1='test_common';
SET @database_2='soc_common';
SET @tableExistInfo='';
SET @columnExistInfo='';
SET @columnInfo='';
-- 98.調用存儲過程
CALL comparisonTableExist(@database_1, @database_2, @tableExistInfo);
CALL comparisonColumnExist(@database_1, @database_2, @columnExistInfo);
CALL comparisonColumnInfo(@database_1, @database_2, @columnInfo);
SET @info=CONCAT(@tableExistInfo, '\n', @columnExistInfo, '\n', @columnInfo);
-- 99.打印
SELECT @info;
-- 100.如果存在存儲過程則刪除
DROP PROCEDURE IF EXISTS `comparisonColumnInfo`;
DROP PROCEDURE IF EXISTS `comparisonColumnExist`;
DROP PROCEDURE IF EXISTS `comparisonTableExist`;
DROP PROCEDURE IF EXISTS `getColumnInfo`;
DROP PROCEDURE IF EXISTS `getColumnCount`;
DROP PROCEDURE IF EXISTS `getTableCount`;
DROP PROCEDURE IF EXISTS `getdatabaseCount`;
無注釋:
DELIMITER &
DROP PROCEDURE IF EXISTS `getdatabaseCount` &
CREATE DEFINER=`oms`@`localhost` PROCEDURE getdatabaseCount(IN database_name CHAR(20), OUT count_date INT)
BEGIN
DECLARE $sqltext VARCHAR(1000);
SET $sqltext = CONCAT('SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\';');
SET @sqlcounts := $sqltext;
PREPARE stmt FROM @sqlcounts;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET count_date = @count_date;
END
&
DROP PROCEDURE IF EXISTS `getTableCount` &
CREATE DEFINER=`oms`@`localhost` PROCEDURE getTableCount(IN database_name CHAR(20), IN table_name CHAR(200), OUT count_date INT)
BEGIN
DECLARE $sqltext VARCHAR(1000);
SET $sqltext = CONCAT('SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\' and t.`TABLE_NAME` = \'', table_name, '\';');
SET @sqlcounts := $sqltext;
PREPARE stmt FROM @sqlcounts;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET count_date = @count_date;
END
&
DROP PROCEDURE IF EXISTS `getColumnCount` &
CREATE DEFINER=`oms`@`localhost` PROCEDURE getColumnCount(IN database_name CHAR(20), IN table_name CHAR(200), IN column_name CHAR(200), OUT count_date INT)
BEGIN
DECLARE $sqltext VARCHAR(1000);
SET $sqltext = CONCAT('SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\' and t.`TABLE_NAME` = \'', table_name, '\' and t.`COLUMN_NAME` = \'', column_name, '\';');
SET @sqlcounts := $sqltext;
PREPARE stmt FROM @sqlcounts;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET count_date = @count_date;
END
&
DROP PROCEDURE IF EXISTS `getColumnInfo` &
CREATE DEFINER=`oms`@`localhost` PROCEDURE getColumnInfo(IN database_name CHAR(20), IN table_name CHAR(200), IN column_name CHAR(200), IN column_info CHAR(50), OUT result_data CHAR(20))
BEGIN
DECLARE $sqltext VARCHAR(1000);
SET $sqltext = CONCAT('SELECT t.', column_info,' into @column_info FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \'', database_name, '\' and t.`TABLE_NAME` = \'', table_name, '\' and t.`COLUMN_NAME` = \'', column_name, '\';');
SET @sqlcounts := $sqltext;
PREPARE stmt FROM @sqlcounts;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET result_data = @column_info;
END
&
DROP PROCEDURE IF EXISTS `comparisonTableExist` &
CREATE DEFINER=`oms`@`localhost` PROCEDURE comparisonTableExist(IN database_1 CHAR(20), IN database_2 CHAR(20), OUT info MEDIUMTEXT)
BEGIN
DECLARE database_name, table_name CHAR(200);
DECLARE this_info, database_table_no MEDIUMTEXT DEFAULT '';
DECLARE database_count_1, database_count_2, resoult_count INT DEFAULT 0;
DECLARE stopflag INT DEFAULT 0;
DECLARE sql_resoult CURSOR FOR SELECT t.TABLE_SCHEMA, t.TABLE_NAME FROM information_schema.COLUMNS t;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag=1;
CALL getdatabaseCount(database_1, database_count_1);
CALL getdatabaseCount(database_2, database_count_2);
IF (database_count_1 <> 0 AND database_count_2 <> 0) THEN
OPEN sql_resoult;
FETCH sql_resoult INTO database_name, table_name;
WHILE (stopflag=0) DO
BEGIN
IF (database_name=database_1 AND INSTR(database_table_no, CONCAT(database_2, '_', table_name)) = 0) THEN
CALL getTableCount(database_2, table_name, resoult_count);
IF (resoult_count = 0) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_2, '的', table_name, '表不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_2, '的', table_name, '表不存在;\n');
END IF;
SET database_table_no=CONCAT(database_table_no, ';', database_2, '_', table_name, ';');
END IF;
ELSE
IF (database_name=database_2 AND INSTR(database_table_no, CONCAT(database_1, '_', table_name)) = 0) THEN
CALL getTableCount(database_1, table_name, resoult_count);
IF (resoult_count = 0) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_1, '的', table_name, '表不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_1, '的', table_name, '表不存在;\n');
END IF;
SET database_table_no=CONCAT(database_table_no, ';', database_1, '_', table_name, ';');
END IF;
END IF;
END IF;
FETCH sql_resoult INTO database_name, table_name;
END;
END WHILE;
CLOSE sql_resoult;
ELSE
IF (database_count_1 = 0 AND database_count_2 = 0) THEN
SET this_info = CONCAT(database_1, '和', database_2, '數據庫不存在或為空數據庫');
ELSE
IF (database_count_1 = 0) THEN
SET this_info = CONCAT(database_1, '數據庫不存在或為空數據庫');
ELSE
SET this_info = CONCAT(database_2, '數據庫不存在或為空數據庫');
END IF;
END IF;
END IF;
SET info=this_info;
END
&
DROP PROCEDURE IF EXISTS `comparisonColumnExist` &
CREATE DEFINER=`oms`@`localhost` PROCEDURE comparisonColumnExist(IN database_1 CHAR(20), IN database_2 CHAR(20), OUT info MEDIUMTEXT)
BEGIN
DECLARE database_name, table_name, column_name CHAR(200);
DECLARE this_info, database_table_no TEXT DEFAULT '';
DECLARE database_count_1, database_count_2, resoult_count INT DEFAULT 0;
DECLARE stopflag INT DEFAULT 0;
DECLARE sql_resoult CURSOR FOR SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.COLUMN_NAME FROM information_schema.COLUMNS t;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag=1;
CALL getdatabaseCount(database_1, database_count_1);
CALL getdatabaseCount(database_2, database_count_2);
IF (database_count_1 <> 0 AND database_count_2 <> 0) THEN
OPEN sql_resoult;
FETCH sql_resoult INTO database_name, table_name, column_name;
WHILE (stopflag=0) DO
BEGIN
IF (database_name=database_1 AND INSTR(database_table_no, CONCAT(database_2, '_', table_name)) = 0) THEN
CALL getTableCount(database_2, table_name, resoult_count);
IF (resoult_count <> 0) THEN
CALL getColumnCount(database_2, table_name, column_name, resoult_count);
IF (resoult_count = 0) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_2, '的', table_name, '表的', column_name, '列不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_2, '的', table_name, '表的', column_name, '列不存在;\n');
END IF;
END IF;
ELSE
SET database_table_no=CONCAT(database_table_no, ';', database_2, '_', table_name, ';');
END IF;
ELSE
IF (database_name=database_2 AND INSTR(database_table_no, CONCAT(database_1, '_', table_name)) = 0) THEN
CALL getTableCount(database_1, table_name, resoult_count);
IF (resoult_count <> 0) THEN
CALL getColumnCount(database_1, table_name, column_name, resoult_count);
IF (resoult_count = 0) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(database_1, '的', table_name, '表的', column_name, '列不存在;\n');
ELSE
SET this_info=CONCAT(this_info, database_1, '的', table_name, '表的', column_name, '列不存在;\n');
END IF;
END IF;
ELSE
SET database_table_no=CONCAT(database_table_no, ';', database_1, '_', table_name, ';');
END IF;
END IF;
END IF;
FETCH sql_resoult INTO database_name, table_name, column_name;
END;
END WHILE;
CLOSE sql_resoult;
END IF;
SET info=this_info;
END
&
DROP PROCEDURE IF EXISTS `comparisonColumnInfo` &
CREATE DEFINER=`oms`@`localhost` PROCEDURE comparisonColumnInfo(IN database_1 CHAR(20), IN database_2 CHAR(20),OUT info MEDIUMTEXT)
BEGIN
DECLARE database_name, table_name, column_name, collation_name, result_data_1, result_data_2, column_info CHAR(200);
DECLARE this_info, database_table_no MEDIUMTEXT DEFAULT '';
DECLARE database_count_1, database_count_2, resoult_count INT DEFAULT 0;
DECLARE stopflag INT DEFAULT 0;
DECLARE sql_resoult CURSOR FOR SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.COLUMN_NAME, t.COLLATION_NAME FROM information_schema.COLUMNS t;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag=1;
CALL getdatabaseCount(database_1, database_count_1);
CALL getdatabaseCount(database_2, database_count_2);
IF (database_count_1 <> 0 AND database_count_2 <> 0) THEN
OPEN sql_resoult;
FETCH sql_resoult INTO database_name, table_name, column_name, collation_name;
WHILE (stopflag=0) DO
BEGIN
IF (database_name=database_1 AND INSTR(database_table_no, CONCAT(database_2, '_', table_name)) = 0) THEN
CALL getTableCount(database_2, table_name, resoult_count);
IF (resoult_count <> 0) THEN
CALL getColumnCount(database_2, table_name, column_name, resoult_count);
IF (resoult_count <> 0) THEN
SET column_info = 'DATA_TYPE';
CALL getColumnInfo(database_1, table_name, column_name, column_info, result_data_1);
CALL getColumnInfo(database_2, table_name, column_name, column_info, result_data_2);
IF (result_data_1 <> result_data_2) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
ELSE
SET this_info=CONCAT(this_info, table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
END IF;
END IF;
SET column_info = 'CHARACTER_SET_NAME';
CALL getColumnInfo(database_1, table_name, column_name, column_info, result_data_1);
CALL getColumnInfo(database_2, table_name, column_name, column_info, result_data_2);
IF (result_data_1 <> result_data_2) THEN
IF (this_info IS NULL OR this_info='') THEN
SET this_info=CONCAT(table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
ELSE
SET this_info=CONCAT(this_info, table_name, '表的', column_name, '列的', column_info, '不一樣;\n');
END IF;
END IF;
END IF;
ELSE
SET database_table_no=CONCAT(database_table_no, ';', database_2, '_', table_name, ';');
END IF;
ELSE
IF (database_name=database_2 AND INSTR(database_table_no, CONCAT(database_1, '_', table_name)) = 0) THEN
CALL getTableCount(database_1, table_name, resoult_count);
IF (resoult_count = 0) THEN
SET database_table_no=CONCAT(database_table_no, ';', database_1, '_', table_name, ';');
END IF;
END IF;
END IF;
FETCH sql_resoult INTO database_name, table_name, column_name, collation_name;
END;
END WHILE;
CLOSE sql_resoult;
END IF;
SET info=this_info;
END
&
DELIMITER ;
SET @database_1='test_common';
SET @database_2='soc_common';
SET @tableExistInfo='';
SET @columnExistInfo='';
SET @columnInfo='';
CALL comparisonTableExist(@database_1, @database_2, @tableExistInfo);
CALL comparisonColumnExist(@database_1, @database_2, @columnExistInfo);
CALL comparisonColumnInfo(@database_1, @database_2, @columnInfo);
SET @info=CONCAT(@tableExistInfo, '\n', @columnExistInfo, '\n', @columnInfo);
SELECT @info;
DROP PROCEDURE IF EXISTS `comparisonColumnInfo`;
DROP PROCEDURE IF EXISTS `comparisonColumnExist`;
DROP PROCEDURE IF EXISTS `comparisonTableExist`;
DROP PROCEDURE IF EXISTS `getColumnInfo`;
DROP PROCEDURE IF EXISTS `getColumnCount`;
DROP PROCEDURE IF EXISTS `getTableCount`;
DROP PROCEDURE IF EXISTS `getdatabaseCount`;
獲取數據庫中表結構:
show columns from t_user
或者
SHOW FULL COLUMNS FROM t_user
