这可能会让你朝着正确的方向前进。
1.创建find_column
存储过程
DROP PROCEDURE IF EXISTS `find_column`;
DELIMITER $$
CREATE PROCEDURE `find_column`(IN i_value varchar(200),
OUT o_columns varchar(2000),
OUT o_message varchar(500))
MAIN_BLOCK : BEGIN
DECLARE is_numeric boolean;
CHECK_NUMERIC : BEGIN
set is_numeric = i_value REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
END CHECK_NUMERIC;
FIND_IT : BEGIN
DECLARE bNoMoreRows BOOLEAN DEFAULT FALSE;
DECLARE v_schema varchar(64);
DECLARE v_table varchar(64);
DECLARE v_column varchar(64);
DECLARE v_data_type varchar(64);
DECLARE v_count int;
-- all schemas, tables and columns in DB
DECLARE columns CURSOR FOR
select table_schema,table_name,column_name,data_type from information_schema.columns;
DECLARE EXIT HANDLER for SQLEXCEPTION set o_message := concat('Unexpected error while trying to find schema, table and column for value : ',i_value);
declare continue handler for not found set bNoMoreRows := true;
open columns;
set o_columns = "";
COLUMN_LOOP: loop
fetch columns
into v_schema,v_table,v_column,v_data_type;
if (
(v_data_type in ('int','bigint','tinyint','decimal','smallint','mediumint') and is_numeric=1)
or (v_data_type not in ('int','bigint','tinyint','decimal','smallint','mediumint') and is_numeric=0)
)
then
SET @dyn_sql=CONCAT('select count(*) into @c from `',v_schema,'`.`',v_table,'` where `',v_column,'`=?');
SET @c = 0;
SET @v_value = i_value;
PREPARE stmt FROM @dyn_sql;
EXECUTE stmt using @v_value;
DEALLOCATE PREPARE stmt;
SET v_count = @c;
if v_count > 0 then
if length(o_columns <= 1800) then
set o_columns = concat(o_columns,",",v_schema,".",v_table,".",v_column);
end if;
end if;
end if;
if bNoMoreRows then
set o_columns = substring(o_columns,2);
close columns;
leave COLUMN_LOOP;
end if;
END loop COLUMN_LOOP;
END FIND_IT;
END MAIN_BLOCK$$
DELIMITER ;
2.用你的值调用find_column
存储过程
call `find_column`('whatever',@columns,@message);
3.查看结果
select @columns;
is_numeric 位是对JBB这篇文章的回答的精心抄袭。
它并不完美(如果您的值存在的列数超过 10 左右会发生什么?如果是这种情况,那么这将只返回前 10 个左右的列(取决于 schema.table.column 名称的长度)字符串是)。
希望它能让你朝着正确的方向前进。
你是对的。你是数据库管理员会对你不满意。但是,如果您不时不时惹恼他们,那么恕我直言,您还不够努力;-)
祝你好运。