⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 车站光标练习_todate比较时间.txt

📁 orale培训教材包括了所有的sql说明和实例
💻 TXT
字号:
--表结构和数据参见  车站综合练习.txt

--查询指定车次的所有站使用一个标准参数光标,在这里略去。


--查询某一车次的所有站,及其终点起点,

--没有车站信息!有bus信息
insert into bus values('120',to_date('6:00:00','hh24:mi:ss'),to_date('23:00:00','hh24:mi:ss'),
1,'北京西站','中关村');


--查询某一车次的所有站,及其终点起点,
create or replace procedure getstation(p_busno in varchar2) is 

  v_qidian bus.qidian%TYPE;
  v_zhongdian bus.zhongdian%TYPE;
  v_station busstation.station%TYPE;
  cursor c_station(busno varchar2) is select station from busstation
  where busno=p_busno;
begin
  
  --查询车次的终点起点
  select qidian,zhongdian into v_qidian ,v_zhongdian from bus where busno=p_busno;
  dbms_output.put_line ( p_busno || '路车 ' || '起点站:' || v_qidian || ',终点站: '|| v_zhongdian);

  open c_station(p_busno);
  
  --第一次fetch纪录,如busstation中没有这次车的车站信息。光标对应集为空
  fetch c_station into v_station;
  if c_station%notfound then
      dbms_output.put_line('没有这次车的车站信息'); 
  else 
    --如果有纪录,输出。接着循环输出其它各站
    dbms_output.put_line(v_station); 

    loop
      fetch c_station into v_station;
      if c_station%notfound then
          exit ;
      end if;
      dbms_output.put_line(v_station);
    end loop;    
  end if;

  
  close c_station;
end;

--本过程未处理当没有给定车次的情况

--查询从A站到B站能坐的所有车--------------------------------------------------
--p_station1,p_station2为给定的车站。由此2参数建立一个2参数的光标。
create or replace procedure findbus (p_station1 in varchar2,p_station2 in varchar2)
is
  cursor c_station (st1 varchar2,st2 varchar2) 
  is select busno from busstation where station = st1 
  intersect
  select  busno from busstation where station = st2;
  --2参数的光标取出过st1,也过st2的集合

  v_bus busstation.busno%TYPE; 
begin
  
  open c_station(p_station1,p_station2);
  fetch c_station into v_bus;
  if c_station%notfound then 
    dbms_output.put_line('没有车可以到达');
  else
    dbms_output.put_line('可以到达的车如下:');
    dbms_output.put_line(v_bus);
    loop
      fetch c_station into v_bus;
      exit when c_station%notfound;
      dbms_output.put_line(v_bus);
    end loop;
  end if;
end;

--测试
call findbus('a','b');
call findbus('中关村','白石桥');



--现在能有车A站到B站能坐的所有车吗-------------------------------------------------


--建立函数:sysdate是否在指定车次首末车时间之间
-- return 0  没有  1 有
create or replace function in_shou_mo (p_busno varchar2) return number is
  r_bus bus%rowtype;
begin
  select * into r_bus from bus where busno=p_busno;
  if r_bus.shouban < to_date(to_char(sysdate,'hh24:mi:ss'),'hh24:mi:ss' )
     and r_bus.moban > to_date(to_char(sysdate,'hh24:mi:ss') ,'hh24:mi:ss') then 
    --sysdate在首末车时间之间
    return 1;
   else
    return 0;
   end if;
end;


-------------------使用to_char()比较-----------------------
create or replace function in_shou_mo (p_busno varchar2) return number is
  r_bus bus%rowtype;
  --r_bus为bus表结构相同的纪录
begin
  select * into r_bus from bus where busno=p_busno;
  if to_char(r_bus.shouban,'hh24:mi:ss') < to_char(sysdate,'hh24:mi:ss')
     and to_char(r_bus.moban,'hh24:mi:ss') > to_char(sysdate,'hh24:mi:ss')  then 
    --sysdate在首末车时间之间
    return 1;
   else
    return 0;
   end if;
end;





--更改末班时间测试
insert into bus values('301',to_date('5:30:00','hh24:mi:ss'),to_date('20:10:00','hh24:mi:ss'),
1,'农展馆','巴沟村');

select in_shou_mo('301') from dual;


----------------------------------------------存储过程 调用函数in_shou_mo-------------------
create or replace procedure findbus_now (p_station1 in varchar2,p_station2 in varchar2)
is
  cursor c_station (st1 varchar2,st2 varchar2) 
  is select busno from busstation where station = st1 
  intersect
  select  busno from busstation where station = st2;
  --2参数的光标取出过st1,也过st2的集合  

  v_bus busstation.busno%TYPE; 
  flag number:=-1; --标志 检查sysdate是否在指定车次首末车时间之间
  finded number:=0; --标志 判断是否有最终结果。
begin
  
  open c_station(p_station1,p_station2);
  fetch c_station into v_bus;
  if c_station%notfound then 
    dbms_output.put_line('没有车可以到达');
    --没有车可以直达p_station1--p_station2
  else
    --有车可以到达p_station1--p_station2,检查sysdate是否在指定车次首末车时间之间
    flag:=in_shou_mo(v_bus);

    if flag=1 then 
      --sysdate是在指定车次首末车时间之间
      dbms_output.put_line(v_bus || '可以到达');
      finded:=1; --设定找到标志为1
    end if;
    
    --循环检查除第一条纪录以外的其它纪录,是否满足sysdate在本车次首末车时间之间
    loop
      fetch c_station into v_bus;
      exit when c_station%notfound;
      flag :=in_shou_mo(v_bus);  --检查sysdate是否在指定车次首末车时间之间
      if flag=1 then 
        dbms_output.put_line(v_bus || '可以到达');
        finded:=1; --设定找到标志为1
      end if;
    end loop;
    if finded=0 then
      --找到标志为0  
      dbms_output.put_line('现在没有车可以到达'); 
      --有车可以到达p_station1--p_station2,但sysdate都不在指定车次首末车时间之间
    end if;
  end if;
  
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -