频道直达 - 专题 - 新闻 - 基础 - 高级 - 安装 - 技巧 - 数据库 - 手册 - PHP - Linux - Java - MySQL - Apache - 麻辣堂 - 狼盟 - Rails社群 - 搜索 - 下载 - 开源 - 论坛
PHP开发资源网 主页>>数据库相关>> 收藏此文 | 收藏本站 | 设为首页

一个PostgreSQL存储过程的例子

来源:www.phpres.com 作者:Angelover 出处:www.phpres.com 2007-7-23 20:12:33 进入讨论组
关 键 词: 一个PostgreSQL存储过程的例子

需求:
    给出如下条件进行批处理编排
    - 开始日期时间
    - 重复间隔(分钟)
    - 重复次数
    要求在档期内重复安排节目播出, 比如: 2003.01.01 08:00 开始每隔240分钟播出一次, 一共播出100次

    数据库表格(CO_SCHEDULE)
    ------------------------------
    N_PROGID        INT
    DT_STARTTIME    TIMESTAMP
    DT_ENDTIME        TIMESTAMP


存储过程的实现:

create table co_schedule(n_progid int,dt_starttime timestamp,dt_endtime timestamp);

//创建函数:
create function add_program_time(int4,timestamp,int4,int4,int4) returns bool as '
declare
    prog_id alias for $1;
    duration_min alias for $3;
    period_min alias for $4;
    repeat_times alias for $5;
    i int;
    starttime timestamp;
    ins_starttime timestamp;
    ins_endtime timestamp;
begin
    starttime :=$2;
    i := 0;
    while i<repeat_times loop
        ins_starttime := starttime;
        ins_endtime := timestamp_pl_span(ins_starttime,duration_min || ''mins'');
        starttime := timestamp_pl_span(ins_starttime,period_min || ''mins'');
        insert into co_schedule values(prog_id,ins_starttime,ins_endtime);
        i := i+1;
    end loop;
    if i<repeat_times then
        return false;
    else
        return true;
    end if;
end;
'language 'plpgsql';

//执行函数:
select add_program_time(1,'2002-10-20 0:0:0','5','60','5');

//查看结果:select * from co_schedule;
n_progid |      dt_starttime      |       dt_endtime       
----------+------------------------+------------------------
        1 | 2002-10-20 00:00:00+08 | 2002-10-20 00:05:00+08
        1 | 2002-10-20 01:00:00+08 | 2002-10-20 01:05:00+08
        1 | 2002-10-20 02:00:00+08 | 2002-10-20 02:05:00+08
        1 | 2002-10-20 03:00:00+08 | 2002-10-20 03:05:00+08
        1 | 2002-10-20 04:00:00+08 | 2002-10-20 04:05:00+08

ps:
1.数据库一加载 plpgsql语言。如没有,
su - postgres
createlang plpgsql dbname
2.至于返回类型为bool,是因为我不知道如何让函数不返回值。等待改进。
欢迎进入PHP开发资源论坛讨论。
收藏此文】【 】【打印】【关闭
相关文章
图文推荐
论 坛 资 源
PHP开发资源网奋斗目标
阅读排行:
热门技术文档
最新图文档
本站编辑推荐:(本站开通Delphi4PHP专区,欢迎进入论坛交流!)
编缉最近更新文章
网站赞助商
搜索您感兴趣的内容
 
   网站首页 -  网站地图 -  网站合作 -  手册中心 -  通用网址 -  网站论坛 -  网站投稿 -  友情链接 -  帮助中心
版权所有:PHP开发资源网 © 2003-2008 通用网址:PHP资源网 合作媒体: 赛迪网IT技术
互联网违法和不良信息举报中心 | 不良信息举报信箱