PostgreSQL 存储过程/函数

1.有用的链接

postgresql 常用小函数

Postgresql数据库的一些字符串操作函数

PostgreSQL function里面调用function

PostgreSQL学习手册(函数和操作符<二>)

PostgreSQL的存储过程简单入门

2.建立块环境(执行环境)

do language plpgsql $$  
declare  
begin  
...  
..  
.  
end $$;  

 do language plpgsql $$ 
        declare           
        today date  :=now();
        yesterday date;   
        beginDay date := '2012-12-12';    
        val date;
    i int;
    i_str varchar(50);
        begin
        --在存储过程里调用需要使用 perform关键字
    perform InitsFunction() ;
        if(today is null) then         
            RAISE  NOTICE  ' today is null' ;
            else
            RAISE  NOTICE  ' today is not null' ;
        end if;
               
        end;
$$ ;

3. 建立存储过程

create or replace function InitstandardCheckInDays() returns void  as
$body$
    declare 
        today date  ;
        beginDay date;
        val date;
        i int;
    begin
        today  := current_date;
        if not exists(select 1 from standardCheckInDays) then
            beginDay := '2012-12-12';  
        ELSEIF (not exists (select 1 from standardCheckInDays where stCheckInDate=today)) then
            beginDay = (select max(stCheckInDate) from standardCheckInDays)  ;
        end if;

        if(beginDay is  null )then
            RAISE  NOTICE  '当天的数据已存在标准签到表里'     ;
            
        else
            RAISE  NOTICE  '当天的数据不存在标准签到表里'  ;
            RAISE  NOTICE  '初始化或者30年后了!当天的数据不存在标准签到表里'  ;
            for i in 1..100*100 loop 
                
                val := beginDay + i ;
                insert into standardCheckInDays(stCheckInDate)
                values(val) ;
                RAISE  NOTICE  ' %' , val;
            end loop;
        end if;
         
    end;
$body$  language plpgsql;
select '当前日期:' || to_char(now(),'YYYY-MM-DD HH24:MI:SS.MS');
原文地址:https://www.cnblogs.com/zhshlimi/p/6656520.html