【问题标题】:Error "PLS-00302: component 'MIN' must be declared" trying to find min/max value错误“PLS-00302:必须声明组件'MIN'”试图找到最小/最大值
【发布时间】:2022-01-01 12:58:09
【问题描述】:

在尝试查找距离旅行的最小值/最大值时,我收到错误“PLS-00302:必须声明组件'MIN'”。有什么想法吗?

create or replace procedure longandshortdist (p_distance in number)
is
  cursor longshortcursor is 
    select source_town, destination_town, distance
    from distances
    where distance = p_distance;
  distance_row longshortcursor%rowtype;

begin
  for distance_row in longshortcursor
  loop
    dbms_output.put_line('source town is: ' || distance_row.source_town || 'destination 
    town is: ' || distance_row.destination_town || 'shortest trip is: ' || 
    distance_row.min(distance) || 'longest trip is: ' || distance_row.max(distance));
    
  end loop;
end;

我得到的错误代码:

12/1      PL/SQL: Statement ignored
12/169    PLS-00302: component 'MIN' must be declared
Errors: check compiler log

【问题讨论】:

  • 你不能这样使用MINMAX;它们是在 SQL 语句(而不是 PL/SQL)中工作的聚合函数,通常不会将它们应用于单行。请edit 使用minimal reproducible example 提出您的问题,包括为您的表添加CREATE TABLE 语句;样本数据的INSERT 语句;以及您的程序的预期输出,以便我们了解您想要实现的目标。
  • 我认为您可能对这里的逻辑有些困惑。您的过程接受一个参数,然后在 where 子句中使用它:where distance = p_distance; 所以从该游标返回的每一行都将具有相同的距离。 MIN 和 MAX 在这方面是有意义的。

标签: sql oracle plsql


【解决方案1】:

或 我认为您正在尝试获取两个值的最小值/最大值,如果是这样,请查看以下链接: max function greatest

min function least

【讨论】:

  • 虽然这些链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • @Tyler2P 谢谢你,我会考虑你的评论以应对更多类似情况
【解决方案2】:

这是因为 minmax 是在查询中使用的聚合函数,而不是在块内。

例子:

SELECT max(a.f) max, min(a.f) min
FROM (
select 1 f from dual
union all
select 2 f from dual
union all
select 3 f from dual) a

结果:

MAX MIN
3 1

【讨论】:

    【解决方案3】:

    读取 cmets;似乎非常有用。同时,我试图弄清楚你想要什么,这可能就是“它”。

    样本数据为

    SQL> SELECT *
      2      FROM distances
      3  ORDER BY distance;
    
    SOURCE_TOW DESTINAT   DISTANCE
    ---------- -------- ----------
    Zagreb     Karlovac         40
    Zadar      Split           120
    Koprivnica Osijek          200
    

    过程不接受任何参数(因为,您只会获取距离等于 p_distance 的行(再次参见 Connor 的评论))并循环遍历表中的所有行。

    循环本身会显示所有城镇,但 MIN 和 MAX 距离仅显示一次 - 在循环之外,一旦您实际计算它们的值。

    SQL> CREATE OR REPLACE PROCEDURE longandshortdist
      2  IS
      3     mindist  NUMBER := 1E6;
      4     maxdist  NUMBER := 0;
      5  BEGIN
      6     FOR cur_r
      7        IN (SELECT source_town, destination_town, distance FROM distances)
      8     LOOP
      9        mindist := LEAST (mindist, cur_r.distance);
     10        maxdist := GREATEST (maxdist, cur_r.distance);
     11
     12        DBMS_OUTPUT.put_line (
     13              'source town is: '
     14           || cur_r.source_town
     15           || ', destination town is: '
     16           || cur_r.destination_town
     17           || ', distance is: '
     18           || cur_r.distance);
     19     END LOOP;
     20
     21     DBMS_OUTPUT.put_line (
     22        'shortest trip is: ' || mindist || ', longest trip is: ' || maxdist);
     23  END;
     24  /
    
    Procedure created.
    

    测试:

    SQL> SET SERVEROUTPUT ON
    SQL> EXEC longandshortdist;
    source town is: Zagreb, destination town is: Karlovac, distance is: 40
    source town is: Zadar, destination town is: Split, distance is: 120
    source town is: Koprivnica, destination town is: Osijek, distance is: 200
    shortest trip is: 40, longest trip is: 200
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2021-01-06
      • 2015-04-26
      相关资源
      最近更新 更多