PostgreSQL數據庫PL/PGSQL學習使用

基本結構

create or replace function somefunc() 
returns varchar as 
$$
declare
   name varchar := 'wangzhen';
begin
   return name;
end   
$$ language plpgsql ;

$$做用

  • 函數代碼在function中實際上爲一個字符串,代碼1和代碼2等價,但代碼中字符串的單引號須要寫兩個進行轉義。
-- 代碼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 ;
  • "美圓符引用」書寫字符串常量,使單引號、反斜線、$符等按照字面值進行解釋,不須要寫兩個或在4個或着更多進行轉義,代碼3和代碼4等價。
--代碼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和代碼6和代碼7等價。
--代碼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和9等價
--代碼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;

執行SQL

  • 沒有返回結果的SQL能夠直接執行
--代碼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;
  • 不須要返回結果的select能夠經過perform執行。perform替換select
--代碼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;
  • 執行動態拼接SQL
--代碼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 做用

返回結果集合(多行)時,須要使用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;
  • 返回結果集時PostgreSQL 9.5.4,須要使用自定義類型或表類型制定返回類型,不能用record匿名類型。

循環判斷

  • LOOP循環
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 ;
  • WHILE循環
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 ;
  • FOR循環
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;
  • FOREACH循環
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;
相關文章
相關標籤/搜索