动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   PHP → 阅读文章

 一个PostgreSQL存储过程的例子:

作者来源: 
阅读 数 143 人次 , 2006-4-18 16:48:00 


   需求:
    给出如下条件进行批处理编排
    - 开始日期时间
    - 重复间隔(分钟)
    - 重复次数
    要求在档期内重复安排节目播出, 比如: 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,是因为我不知道如何让函数不返回值。等待改进。

    
 本文Tags存储过程  数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:MySQL数据导入与导出 [转]
· 下一篇:Oracle 和 mysql 的一些简单命令对比参照
· MySQL数据库函数详解(2)
· MySQL数据库函数详解(3)
· PHP4 的可选加载模块
· 随机广告显示(PHP函数)
· Windows XP系统网络功能两则优化技巧


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077