create or replace function somefunc()
returns varchar as
$$
declare
name varchar := 'wangzhen';
begin
return name;
end
$$ language plpgsql ;
-- 代码1
create or replace function somefunc()
returns varchar as
$$
declare
name varchar := 'wangzhen';
begin
return name;
end
$$ language plpgsql ;
--代码2
create or replace function somefunc()
returns varchar as
'
declare
name varchar := ''wangzhen'';
begin
return name;
end
' language plpgsql ;
--代码3
create or replace function somefunc()
returns varchar as
$$
declare
name varchar := 'wangzhen''blog';
begin
return name;
end
$$ language plpgsql ;
--代码4
create or replace function somefunc()
returns varchar as
$$
declare
name varchar := $tag$wangzhen'blog$tag$;
begin
return name;
end
$$ language plpgsql ;
--代码5
create or replace function somefunc()
returns varchar as
$body$
declare
name varchar := $tag$wangzhen'blog$tag$;
begin
return name;
end
$body$ language plpgsql ;
--代码6
create or replace function somefunc()
returns varchar as
$func$
declare
name varchar := $$wangzhen'blog$$;
begin
return name;
end
$func$ language plpgsql ;
--代码7
create or replace function somefunc()
returns varchar as
$$
declare
name varchar := 'wangzhen''blog';
begin
return name;
end
$$ language plpgsql ;
--代码8
create or replace function somefunc()
returns varchar as
$$
<>
declare
name varchar := 'wangzhen';
begin
return outblock.name;
end;
$$ language plpgsql;
--代码9
create or replace function somefunc()
returns varchar as
$$
declare
name varchar := 'wangzhen';
begin
return name;
end;
$$ language plpgsql;
--代码10
create or replace function somefunc()
returns varchar as
$$
<>
declare
name varchar := 'wangzhen';
begin
<>
declare
name varchar := 'xiaozhang';
begin
outblock.name = innerblock.name;
end;
return outblock.name;
end;
$$ language plpgsql;
abase=# select somefunc();
somefunc
-----------
xiaozhang
--代码11
create or replace function somefunc(name varchar)
returns varchar as
$$
<>
declare
name varchar := somefunc.name;
begin
return outblock.name;
end;
$$ language plpgsql;
abase=# select somefunc('xiaoli');
somefunc
----------
xiaoli
(1 row)
--代码12
create or replace function somefunc(newname varchar)
returns varchar as
$$
declare
name varchar := newname;
begin
return name;
end;
$$ language plpgsql;
--代码13
create or replace function somefunc(varchar,varchar)
returns varchar as
$$
declare
newname alias for $1;
name varchar := newname;
name2 varchar := $2;
begin
return name;
end;
$$ language plpgsql;
--代码14
create or replace function somefunc2()
returns void as
$$
begin
create table test1 (n_id integer,c_mc varchar(300));
insert into test1 (n_id,c_mc) values (1,'wangzhen');
end;
$$ language plpgsql;
--代码15
create or replace function somefunc3()
returns void as
$$
begin
perform * from test1;
end;
$$ language plpgsql;
--代码16
create or replace function somefunc3()
returns void as
$$
begin
perform somefunc();
--perform 怎么处理with和其他复杂查询?
end;
$$ language plpgsql;
--代码17
create or replace function somefunc3()
returns void as
$$
declare
var_sql varchar := 'insert into test1 values (0,'||quote_literal('admin')||')';
begin
execute var_sql;
execute format('insert into %I values (%L,%L)','test1',2,'xiaoniu');
end;
$$ language plpgsql;
返回结果分为返回标量(单行)和返回结果集合(多行)两种情况。
-- 代码18
-- 返回单行int
create or replace function somefunc4()
returns int as
$$
begin
return 100;
end
$$ language plpgsql;
-- 代码19
-- 通过out参数返回
create or replace function somefunc4(out v1 integer)
as
$$
begin
v1 := 100;
end
$$ language plpgsql;
-- 代码20
-- 返回单行record 复合类型
create or replace function somefunc4(id integer,mc varchar)
returns record as
$$
declare
r record;
begin
r := row(id,mc);
return r;
end
$$ language plpgsql;
SETOF 作用
返回结果集合(多行)时,需要使用SETOF指定
--代码21
--返回单列多行
create or replace function somefunc5()
returns setof int as
$$
declare
id int;
begin
for id in select n_id from test1
loop
return next id;
end loop;
return;
end
$$ language plpgsql;
结果:
abase=# select somefunc5();
somefunc5
-----------
1
1
0
0
2
(5 rows)
-- 代码22
-- 通过out参数返回
-- returns setof record 可以省略
create or replace function somefunc6(out id integer,out name varchar)
returns setof record as
$$
declare
r record;
begin
for r in select n_id,c_mc from test1
loop
id := r.n_id;
name := r.c_mc;
return next;
end loop;
end
$$ language plpgsql;
结果:
abase=# select * from somefunc6() ;
id | name
----+-----------
1 | wangzhen
1 | xiaozhang
0 | admin
0 | admin
2 | xiaoniu
(5 rows)
-- 代码23
-- 通过return next 返回自定义类型
create type test_rs as (id int,mc varchar);
create or replace function somefunc7()
returns setof test_rs as
$$
declare
r test_rs%rowtype;
begin
for r in select n_id,c_mc from test1
loop
return next r;
end loop;
return;
end
$$ language plpgsql;
-- 代码24
-- 通过return query 返回自定义类型
create or replace function somefunc7()
returns setof test_rs as
$$
declare
r test_rs%rowtype;
begin
return query select n_id,c_mc from test1;
return;
end
$$ language plpgsql;
-- 代码25
-- 通过return next返回表类型
create or replace function somefunc7()
returns setof test1 as
$$
declare
r test1%rowtype;
begin
for r in select n_id,c_mc from test1
loop
return next r;
end loop;
return;
end
$$ language plpgsql;
-- 代码26
-- 通过拼接sql return next 返回自定义类型
create or replace function somefunc7()
returns setof test_rs as
$$
declare
r test_rs%rowtype;
begin
for r in execute 'select n_id,c_mc from test1'
loop
return next r;
end loop;
return;
end
$$ language plpgsql;
-- 代码26
-- 通过拼接sql return query 返回自定义类型
create or replace function somefunc7()
returns setof test_rs as
$$
declare
r test_rs%rowtype;
begin
return query execute 'select n_id,c_mc from test1';
return;
end
$$ language plpgsql;
create or replace function testloop1()
returns void as
$$
declare
count int :=0;
begin
loop
exit when count >=100;
count := count + 1;
raise notice 'count is %',count;
end loop;
end
$$ language plpgsql ;
create or replace function testloop2()
returns void as
$$
declare
count int :=0;
begin
while count <100 loop
count := count + 1;
raise notice 'count is %',count;
end loop;
end
$$ language plpgsql ;
create or replace function testloop3()
returns void as
$$
begin
for i in 1..100 loop
raise notice 'count is %',i;
end loop;
end
$$ language plpgsql;
create or replace function testloop4()
returns void as
$$
begin
for i in reverse 100..1 by 2 loop
raise notice 'count is %',i;
end loop;
end
$$ language plpgsql;
create or replace function testloop5()
returns void as
$$
declare
v_arr int[]:= array[1,2,3,4,5];
i int;
begin
foreach i in array v_arr loop
raise notice 'count is %',i;
end loop;
end
$$ language plpgsql;