ITPub博客

首页 > 数据库 > Oracle > 回复: 求一个关于排期的存储过程, 感谢大家

回复: 求一个关于排期的存储过程, 感谢大家

原创 Oracle 作者:longwansheng 时间:2007-03-09 15:45:00 0 删除 编辑

现在有一个有关未用排期的问题, 现在预约表AAA, 存有一个预约开始时间STARTDATE, 预约结束时间ENDDATE, 表内有很多信息,

比如一条信息是 开始时间2007.2.15,结束时间2007.4.1, 另一条数据是开始时间2007.5.20,结束时间2007.8.10, 我现在想知道的是, 我给一个时间段, 比如2007.1---2007.6, 未用的时间有哪些, 我需要的结果是

2007.1.1至2007.2.14
2007.4.2至2007.5.19

http://www.itpub.net/showthread.php?s=&postid=7026685#post7026685

[@more@]

/*
create table sche_test (start_date date,end_date date)

insert into sche_test values(sysdate,sysdate+5);

insert into sche_test values(sysdate+9,sysdate+15);

insert into sche_test values(sysdate+12,sysdate+32);

insert into sche_test values(sysdate+30,sysdate+45);

insert into sche_test values(sysdate+60,sysdate+85);
*/
create or replace function sche_date_test return varchar2 is
min_date date;
max_date date;
curr_date date;
v_return varchar2(3000);

cursor cur is
select trunc(start_date) start_date,trunc(end_date) end_date
from sche_test
order by 1,2;

begin

select trunc(sysdate),min(start_date) min_date,max(end_date) max_date
into curr_date,min_date,max_date
from sche_test;

for rec in cur loop
if curr_date<=rec.start_date-1 then
v_return:=v_return||to_char(curr_date,'yyyymmdd')||'-'||to_char(rec.start_date-1,'yyyymmdd')||' ; ';
end if;
curr_date:=rec.end_date+1;

end loop;
if v_return is null then
v_return:='null';
end if;

return v_return;

exception
when others then
return 'null';

end;

/*
select sche_date_test() from dual

20070315-20070317 ; 20070424-20070507 ;
*/

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/92289/viewspace-903642/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-12

  • 博文量
    162
  • 访问量
    774135