drop procedure sp_findfrom_list;
create procedure sp_findfrom_list(
	a_table_name varchar(50),
	a_filter_column varchar(50),
	filter_by_value varchar(50),
	a_addtional_where_clause varchar(500)
	)
BEGIN

DECLARE v_return_sql TEXT;
DECLARE v_table_columns varchar(1000);
DECLARE v_schema_name TEXT;

SELECT 	lv.description 
INTO	v_schema_name
FROM	lookups l,
		lookup_values lv
WHERE	l.id = lv.lookup_id
AND		lv.short_code = 'APP_SCHEMA_NAME'
AND		lv.is_active = 'Y';

SELECT	GROUP_CONCAT(column_name)
INTO	v_table_columns
FROM 	INFORMATION_SCHEMA.COLUMNS
WHERE 	table_schema = v_schema_name
AND		TABLE_NAME = a_table_name ;

SET v_return_sql = FN_BuildSQLQuery(v_table_columns, a_table_name, a_filter_column, filter_by_value, a_addtional_where_clause);

PREPARE sql_statement from v_return_sql;
EXECUTE sql_statement;
DEALLOCATE PREPARE sql_statement;

end;
