Search through all databases, tables, columns in mysql

Developed In: SQL — Contributed by: kedar

This stored procedure is tested on mysql 5.0.83-community-nt-log on windows machine successfully. Summary of snippet: - Create a table for storing output. - Loop through information_schema database's COLUMNS table to obtain all databases, table and column names. - Execute a count(*) query on database.table for each column with appropriate search string in where condition. - If count(*) > 0, that perticular column has the search term. - Insert that triplet (database name, table name, column name) in to a table. - Select * from table to view respective database,table and column names having the search term.

[Ref: http://kedar.nitty-witty.com/miscpages/mysql-search-through-all-database-tables-columns-stored-procedure.php ]


kedar
SQL
  1. ## Table for storing resultant output
  2.  
  3. CREATE TABLE `temp_details` (
  4. `t_schema` varchar(45) NOT NULL,
  5. `t_table` varchar(45) NOT NULL,
  6. `t_field` varchar(45) NOT NULL
  7. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  8.  
  9.  
  10.  
  11.  
  12.  
  13. ## Procedure for search in all fields of all databases
  14. DELIMITER $$
  15. #Script to loop through all tables using Information_Schema
  16. DROP PROCEDURE IF EXISTS get_table $$
  17. CREATE PROCEDURE get_table(in_search varchar(50))
  18. READS SQL DATA
  19. BEGIN
  20. DECLARE trunc_cmd VARCHAR(50);
  21. DECLARE search_string VARCHAR(250);
  22.  
  23. DECLARE db,tbl,clmn CHAR(50);
  24. DECLARE done INT DEFAULT 0;
  25. DECLARE COUNTER INT;
  26.  
  27. DECLARE table_cur CURSOR FOR
  28. SELECT concat('SELECT COUNT(*) INTO @CNT_VALUE FROM `',table_schema,'`.`',table_name,'` WHERE `', column_name,'` REGEXP ''',in_search,''';')
  29. ,table_schema,table_name,column_name
  30. FROM information_schema.COLUMNS
  31. WHERE TABLE_SCHEMA NOT IN ('information_schema','test','mysql');
  32.  
  33. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
  34.  
  35. #Truncating table for refill the data for new search.
  36. PREPARE trunc_cmd FROM "TRUNCATE TABLE temp_details;";
  37. EXECUTE trunc_cmd ;
  38.  
  39. OPEN table_cur;
  40. table_loop:LOOP
  41. FETCH table_cur INTO search_string,db,tbl,clmn;
  42.  
  43. #Executing the search
  44. SET @search_string = search_string;
  45. SELECT search_string;
  46. PREPARE search_string FROM @search_string;
  47. EXECUTE search_string;
  48.  
  49.  
  50. SET COUNTER = @CNT_VALUE;
  51. SELECT COUNTER;
  52.  
  53. IF COUNTER>0 THEN
  54. # Inserting required results from search to table
  55. INSERT INTO temp_details VALUES(db,tbl,clmn);
  56. END IF;
  57.  
  58. IF done=1 THEN
  59. LEAVE table_loop;
  60. END IF;
  61. END LOOP;
  62. CLOSE table_cur;
  63.  
  64. #Finally Show Results
  65. SELECT * FROM temp_details;
  66. END $$
  67. DELIMITER ;

Current Tags

You must be logged in to tag this tool

No Comments yet

Votes

  • Rated 5.00 out of 5
Rated 5.00 out of 5 with 1 votes cast.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment