-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpostgres_function.sql
91 lines (68 loc) · 1.91 KB
/
postgres_function.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
87
88
89
90
91
CREATE OR REPLACE FUNCTION currval(seq_name character varying)
RETURNS bigint AS
$BODY$
DECLARE current INTEGER;
BEGIN
current=0;
select currentValue into current from sequence where seqname=seq_name;
return current;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE FUNCTION getleadertitle(inputorgid VARCHAR(50),orgname VARCHAR(50)) RETURNS varchar(200) as
$BODY$
declare notfound INT DEFAULT 0; --定义一个辅助变量用于判断
declare userid varchar(50);
declare username varchar(50);
declare leadertitle varchar(200);
declare results cursor for
select b.userid,c.username from cmn_org_tb a
left join cmn_orgleader_tb b on a.orgid=b.orgid
inner join cmn_user_tb c on b.userid=c.userid
where a.orgid=inputorgid;
BEGIN
leadertitle =CONCAT('(',orgname);
open results;
if notfound = 1 then
return '';
end if;
LOOP
FETCH results INTO userid,username;
leadertitle = CONCAT_ws(',',leadertitle,username);
END LOOP ;
leadertitle =CONCAT(leadertitle,')');
CLOSE results;
RETURN leadertitle;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE FUNCTION nextval(seq_name VARCHAR(50)) RETURNS bigint as
$body$
BEGIN
UPDATE sequence
SET currentValue = currentValue + increment
WHERE seqname = seq_name;
RETURN currval(seq_name);
END ;
$body$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE FUNCTION setval(seq_name VARCHAR(50), value INTEGER) RETURNS bigint as
$body$
BEGIN
UPDATE sequence
SET currentValue = value
WHERE seqname = seq_name;
RETURN currval(seq_name);
END ;
$body$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE AGGREGATE group_concat(anyelement)
(
sfunc = array_append, -- 每行的操作函数,将本行append到数组里
stype = anyarray, -- 聚集后返回数组类型
initcond = '{}' -- 初始化空数组
);