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 $$ <<outblock>> 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 $$ <<outblock>> declare name varchar := 'wangzhen'; begin <<innerblock>> 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 $$ <<outblock>> 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;
返回結果分爲返回標量(單行)和返回結果集合(多行)兩種狀況。sql
-- 代碼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指定函數
--代碼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;
結果:oop
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;
結果:code
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;