forked from mathieubossaert/central2pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_into_from_refcursor.sql
86 lines (69 loc) · 2.32 KB
/
insert_into_from_refcursor.sql
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
FUNCTION: insert_into_from_refcursor(text, text, refcursor)
description :
-> adapted from https://stackoverflow.com/questions/50837548/insert-into-fetch-all-from-cant-be-compiled/52889381#52889381
Fills the table with data
parameters :
_schema_name text, -- the name of the schema where to create the table
_table_name text, -- the name of the table to create
_ref refcursor -- the name of the refcursor to get data from
returning :
void
*/
CREATE OR REPLACE FUNCTION insert_into_from_refcursor(
_schema_name text,
_table_name text,
_ref refcursor)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
_sql text;
_sql_val text = '';
_sql_col text = '';
_row record;
_hasvalues boolean = FALSE;
BEGIN
LOOP --for each row
FETCH _ref INTO _row;
EXIT WHEN NOT found; --there are no rows more
SELECT _sql_val || '
(' ||
STRING_AGG(
concat(
CASE WHEN val.value::text='null' OR val.value::text='' OR val.value::text='\null' OR val.value::text='"null"'
THEN 'null'
ELSE
concat('''',replace(trim(val.value :: text,'\"'),'''',''''''),'''')
END)
, ',' ORDER BY val.key) ||
'),'
INTO _sql_val
FROM JSON_EACH(TO_JSON(_row)) val;
SELECT _sql_col || STRING_AGG(concat('"',val.key :: text,'"'), ',' ORDER BY val.key)
INTO _sql_col
FROM JSON_EACH(TO_JSON(_row)) val;
_sql_val = TRIM(TRAILING ',' FROM _sql_val);
_sql_col = TRIM(TRAILING ',' FROM _sql_col);
_sql = '
INSERT INTO ' || _schema_name || '.' || _table_name || '(' || _sql_col || ')
VALUES ' || _sql_val ||' ON CONFLICT (data_id) DO NOTHING;';
EXECUTE (_sql);
_sql_val = '';
_sql_col = '';
END LOOP;
--RAISE NOTICE 'insert_into_from_refcursor(): SQL is: %', _sql;
END;
$BODY$;
COMMENT ON function insert_into_from_refcursor(text,text,refcursor)IS '
description :
-> adapted from https://stackoverflow.com/questions/50837548/insert-into-fetch-all-from-cant-be-compiled/52889381#52889381
Fills the table with data
parameters :
_schema_name text, -- the name of the schema where to create the table
_table_name text, -- the name of the table to create
_ref refcursor -- the name of the refcursor to get data from
returning :
void';