Function 函数中的pl/sql返回间隔计算

Function 函数中的pl/sql返回间隔计算,function,plsql,Function,Plsql,我有一些跟踪公交线路、车站和它们的时间表的表格。我试图创建一个函数,返回同一线路上两个公共汽车站之间的行程时间,但我无法使该函数按预期工作 功能代码: create or replace function total_time_func(sch_id in sched.shid%type, stop_start in stops.sname%type, stop_end in stops.sname%type) return interval day to second is

我有一些跟踪公交线路、车站和它们的时间表的表格。我试图创建一个函数,返回同一线路上两个公共汽车站之间的行程时间,但我无法使该函数按预期工作

功能代码:

create or replace function total_time_func(sch_id in sched.shid%type, stop_start in stops.sname%type, stop_end in stops.sname%type)
     return interval day to second is
     t_time interval day to second;
     start_time stop_sched.scheduled_arrival%type;
     end_time stop_sched.scheduled_arrival%type;

begin

     select scheduled_arrival 
     into start_time
     from stop_sched, stops
     where stop_sched.sid = stops.sid
     and stop_sched.shid = sch_id
     and stops.sname = stop_start;

     select scheduled_arrival
     into end_time
     from stop_sched, stops
     where stop_sched.sid = stops.sid
     and stop_sched.shid = sch_id
     and stops.sname = stop_end;

     t_time := end_time - start_time;

     return t_time;

end; 
declare
     total_time interval day to second;

begin 
     total_time := total_time_func(1, '5th', '7th');

     if total_time > 0 
         then dbms_output.put_line('The total time is: ' || total_time);
    else dbms_output.put_line('Stops Not Found');
    end if;
end;
函数编译时不返回错误;但是,只要我运行程序调用函数返回的值,我就会收到一个错误

调用函数返回值的程序:

create or replace function total_time_func(sch_id in sched.shid%type, stop_start in stops.sname%type, stop_end in stops.sname%type)
     return interval day to second is
     t_time interval day to second;
     start_time stop_sched.scheduled_arrival%type;
     end_time stop_sched.scheduled_arrival%type;

begin

     select scheduled_arrival 
     into start_time
     from stop_sched, stops
     where stop_sched.sid = stops.sid
     and stop_sched.shid = sch_id
     and stops.sname = stop_start;

     select scheduled_arrival
     into end_time
     from stop_sched, stops
     where stop_sched.sid = stops.sid
     and stop_sched.shid = sch_id
     and stops.sname = stop_end;

     t_time := end_time - start_time;

     return t_time;

end; 
declare
     total_time interval day to second;

begin 
     total_time := total_time_func(1, '5th', '7th');

     if total_time > 0 
         then dbms_output.put_line('The total time is: ' || total_time);
    else dbms_output.put_line('Stops Not Found');
    end if;
end;
这是我收到的错误:

Error report -
ORA-01403: no data found
ORA-06512: at line 16
ORA-06512: at line 5
01403. 00000 -  "no data found"
*Cause:    No data was found from the objects.
*Action:   There was no data from the objects which may be due to end of fetch.
我不确定我做错了什么,我已经多次重写并从头开始,但都没有成功。值得注意的是,我知道我可能不使用函数就可以实现这一点;然而,我对函数还不熟悉,我希望练习编写函数,而不仅仅是放弃

以下是我的表格供参考:

create table lines 
(lid int, 
lname varchar(30),  
num_station int, 
status int,
primary key(lid));

create table stops
(sid int, 
sname varchar(30), 
address varchar(100), 
status int, 
primary key(sid));

create table stop_line
(lid int, 
sid int,  
seq int, 
primary key(lid,sid),
foreign key(lid) references lines,
foreign key(sid) references stops);

create table sched
(shid int,  
lid int,  
direction int,   
primary key (shid), 
foreign key (lid) references lines);

create table stop_sched 
(shid int, 
sid int, 
scheduled_arrival interval day to second, 
primary key (shid, sid),
foreign key(shid) references sched, 
foreign key(sid) references stops);
作为旁注,我最初还在函数体中包含以下语句:;但是,我无法编译函数而不返回错误:

select shid 
into sch_id
from sched
where sch_id = shid;

select sname
into stop_start
from stops
where stop_start = sname;

select sname
into stop_end
from stops
where stop_end = sname;
添加这些语句时返回的错误:

LINE/COL  ERROR
--------- -------------------------------------------------------------
9/5       PL/SQL: SQL Statement ignored
10/10     PLS-00403: expression 'SCH_ID' cannot be used as an INTO-target of a SELECT/FETCH 
statement
11/5      PL/SQL: ORA-00904: : invalid identifier
14/5      PL/SQL: SQL Statement ignored
15/10     PLS-00403: expression 'STOP_START' cannot be used as an INTO-target of a 
SELECT/FETCH statement
16/5      PL/SQL: ORA-00904: : invalid identifier
19/5      PL/SQL: SQL Statement ignored
20/10     PLS-00403: expression 'STOP_END' cannot be used as an INTO-target of a 
SELECT/FETCH statement
21/5      PL/SQL: ORA-00904: : invalid identifier
Errors: check compiler log
非常感谢您提供的任何指导,因为我在使用函数时遇到了不少麻烦。谢谢

编辑以包含样本表值。stop_sched表的第一行和最后一行应满足参数sch_id=1、stop_start='5th',stop_end='7th',但函数未返回任何数据

insert into stops values(1,'5th', '42 5th Avenue, Buffalo, NY 14201,1);
insert into stops values(2,'10th', ‘889 10th Avenue, Buffalo, NY 14201',1);
insert into stops values(3,'Main', '10 Main Street, Buffalo, NY 14201',1);
insert into stops values(4,'7th', '900 7th Avenue, Buffalo, NY 14201',0);

insert into schedule values(1,1, 1);
insert into schedule values(2,1, 1);

insert into stop_sched values(1, 1, interval '7:30:00.00' hour to second);
insert into stop_sched values(1, 2, interval '7:40:00.00' hour to second);
insert into stop_sched values(1, 3, interval '7:50:00.00' hour to second);
insert into stop_sched values(1, 4, interval '8:10:00.00' hour to second);
这个错误清楚地表明,函数中的查询在执行过程中没有返回任何行和匿名块

代码按预期工作。请参阅下面的演示:

表格准备:

CREATE  TABLE stops (
    sid       INT,
    sname     VARCHAR(30),
    address   VARCHAR(100),
    status    INT
);

insert into stops values (1,'A','DFDFD',1);
insert into stops values (2,'B','FDKJH',3);
insert into stops values (3,'C','IOIOS',4);
insert into stops values (4,'D','LKJJA',5);


CREATE   TABLE stop_sched (
    sid                 INT,
    scheduled_arrival   INTERVAL DAY TO SECOND
);

insert into stop_sched values (1,'3 12:30:06.7');
insert into stop_sched values (2,'4 12:30:06.7');
功能:

CREATE OR REPLACE FUNCTION total_time_func
(
    -- sch_id       IN  number, --sched.shid%TYPE,
     stop_start   IN  varchar2,--stops.sname%TYPE,
     stop_end      IN  varchar2
) 
RETURN INTERVAL DAY TO SECOND IS
    t_time       INTERVAL DAY TO SECOND;
    start_time   stop_sched.scheduled_arrival%TYPE;
    end_time     stop_sched.scheduled_arrival%TYPE;
BEGIN

     select scheduled_arrival 
     into start_time
     from stop_sched
     Inner join  stops
     ON stop_sched.sid = stops.sid
    -- and stop_sched.shid = sch_id
     and stops.sname = stop_start;

     select scheduled_arrival
     into end_time
     from stop_sched
     inner join stops
     ON stop_sched.sid = stops.sid
     --and stop_sched.shid = sch_id
     and stops.sname = stop_end;

    t_time   := end_time - start_time;    

    RETURN t_time;
END;
执行:

select total_time_func('A','B') OUTPUT from dual;
输出:

SQL> /
     OUTPUT 
     -------
     +01 00:00:00.000000



 SQL> 
    DECLARE
        total_time   INTERVAL DAY TO SECOND;
    BEGIN
        total_time   := total_time_func('A', 'B');
        IF
            total_time > 0
        THEN
            dbms_output.put_line('The total time is: ' || total_time);
        ELSE
            dbms_output.put_line('Stops Not Found');
        END IF;

    END;
Stops Not Found
输出:

SQL> /
     OUTPUT 
     -------
     +01 00:00:00.000000



 SQL> 
    DECLARE
        total_time   INTERVAL DAY TO SECOND;
    BEGIN
        total_time   := total_time_func('A', 'B');
        IF
            total_time > 0
        THEN
            dbms_output.put_line('The total time is: ' || total_time);
        ELSE
            dbms_output.put_line('Stops Not Found');
        END IF;

    END;
Stops Not Found

我发现我遇到的问题是程序调用函数,而不是函数本身。我将IF子句更改为以下内容,并按预期执行

IF
        total_time is not null
    THEN
        dbms_output.put_line('The total time is: ' || total_time);
    ELSE
        dbms_output.put_line('Stops Not Found');
    END IF;
使用total_time>0未返回任何数据,因为它的数据类型与从函数返回的变量不同

exception
     when no_data_found
     then dbms_output.put_line('No Data Found');
     return NULL;
我还向函数中添加了以下异常

exception
     when no_data_found
     then dbms_output.put_line('No Data Found');
     return NULL;

几天后,我们将到达2020年,请学习新的连接语法。找不到数据意味着在where子句中具有条件的表中找不到数据。由于我们不了解您的数据,我们无法告诉您您做错了什么。只需逐个运行选择,您就会发现错误。感谢您将其分解。我想我的困惑是,我有一些特定的行,它们应该满足条件,并且在输入这些参数时会返回,所以它没有返回任何数据是出乎意料的。我注意到您在函数中省略了schu_id。是否有您忽略此项的原因,是否会导致函数中的条件匹配出现问题?我还编辑了我的问题以包含示例代码。表中有其他计划,但我只包括sch_id 1值,因为这是表中的sch_idparameters@Rash8151‘你省略了schu-id’-是的,因为我只是在演示。您需要处理连接并检查数据是否存在,以便使用所有表的所有相关连接。