Last active
July 24, 2018 17:39
-
-
Save chales/6d7e2944a8e0859be676 to your computer and use it in GitHub Desktop.
Procedure to search through all databases, tables and columns in MySQL DB Server.
This file contains hidden or 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
# Search through all databases, tables, and columns in a MySQL db. | |
# From http://kedar.nitty-witty.com/blog/search-through-all-databases-tables-columns-in-mysql | |
## Table for storing resultant output | |
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 | |
# Insert results from search | |
INSERT INTO temp_details VALUES(db,tbl,clmn); | |
END IF; | |
IF done=1 THEN | |
LEAVE table_loop; | |
END IF; | |
END LOOP; | |
CLOSE table_cur; | |
# Show Results | |
SELECT * FROM temp_details; | |
END $$ | |
DELIMITER ; |
On one my table I got error:
Error code: 1365, SQLState: 22012, Message: Division by 0
with test query: CALL get_table
('1')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, so then you call
get_table('regex search term')
and it fills thetemp_details
table with where to find the string.