-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_table_columns_string.sql
More file actions
63 lines (48 loc) · 1.35 KB
/
get_table_columns_string.sql
File metadata and controls
63 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- -----------------------------
-- --- GET TABLE COLUMNS STRING
-- -----------------------------
DROP FUNCTION IF EXISTS `get_table_columns_string`;
DELIMITER ;;
CREATE DEFINER=`hqlive`@`%` FUNCTION `get_table_columns_string`(
in_database_name CHAR(50),
in_tab_name CHAR(50),
in_show_table_name BOOL
) RETURNS text CHARSET utf8
begin
DECLARE out_columns TEXT DEFAULT '';
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE t CHAR(50);
DECLARE SQL_stmt TEXT;
-- Create cursor
DECLARE cur_columns CURSOR FOR
SELECT column_name
FROM information_schema.columns
WHERE 1=1
AND table_schema = in_database_name
AND table_name = in_tab_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN cur_columns;
SET @out_columns = '';
the_loop: LOOP
FETCH cur_columns INTO t;
-- break the loop
IF no_more_rows THEN
CLOSE cur_columns;
LEAVE the_loop;
END IF;
-- if we want to display table name
IF in_show_table_name THEN
SET @column = CONCAT('`', in_tab_name, '`.`', t, '`');
-- if we don't want to display table name
ELSE
SET @column = CONCAT('`', t, '`');
END IF;
-- Concatenate columns
SET @out_columns = IF(@out_columns = '', @column, CONCAT(@out_columns, ', ', @column));
END LOOP the_loop;
RETURN @out_columns;
end;;
DELIMITER ;