Create a gist now

Instantly share code, notes, and snippets.

@ciju /summary.sql Secret
Created Sep 7, 2013

What would you like to do?
gives a summary of your mysql database tables
-- ref: http://stackoverflow.com/questions/7737970/procedure-to-loop-through-comma-separated-string-is-not-working
-- ref: http://www.java2s.com/Code/SQL/Procedure-Function/EXECUTEdynamicSQLcommand.htm
delimiter $$
drop procedure if exists execsql$$
create procedure execsql(q varchar(1000))
begin
set @q = q;
prepare stmt from @q;
execute stmt;
deallocate prepare stmt;
end$$
drop procedure if exists showtbl$$
create procedure showtbl(tbl varchar(100))
begin
call execsql(concat("select \"", tbl, "\""));
call execsql(concat("select count(*) as entries from ", tbl));
call execsql(concat("select * from ", tbl, " limit 10"));
end$$
drop procedure if exists showalltbl$$
create procedure showalltbl(strids varchar(1000))
begin
declare strlen int default 0;
declare substrlen int default 0;
declare tbl varchar(100) default "";
if strids is null then
set strids = '';
end if;
do_this:
loop
set strlen = char_length(strids);
set tbl = substring_index(strids, ',', 1);
call showtbl(tbl);
set substrlen = char_length(tbl)+2;
set strids = mid(strids, substrlen, strlen);
if strids = null then
leave do_this;
end if;
if strids = '' then
leave do_this;
end if;
end loop do_this;
end$$
delimiter ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment