1.有用的链接
Postgresql数据库的一些字符串操作函数
PostgreSQL function里面调用function
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();
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 from standardCheckInDays) then
beginDay := '2012-12-12';
ELSEIF (not exists (select 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 ..* 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');