PL/SQL 函数未知编译错误

PL/SQL function unknown compilation error

总初学者 PL/SQL

假设我有 2 个表,

一个是REGION(R_KEY, R_NAME)

还有一个叫做 NATION(N_KEY, N_NAME, N_REGIONKEY),其中 N_REGIONKEY 匹配 R_KEY

中的一个值

例子 在 REGION 我们有

╔═══════╦════════════════╗
║ R_KEY ║     R_Name     ║
╠═══════╬════════════════╣
║     0 ║ AFRICA         ║
║     1 ║ SOUTHEAST ASIA ║
║     2 ║ EUROPE         ║
╚═══════╩════════════════╝

并在 NATION

╔═══════╦═══════════╦═════════════╗
║ N_KEY ║  N_NAME   ║ N_REGIONKEY ║
╠═══════╬═══════════╬═════════════╣
║     0 ║ INDONESIA ║           1 ║
║     1 ║ LONDON    ║           2 ║
║     2 ║ FRANCE    ║           2 ║
║     3 ║ KENYA     ║           0 ║
╚═══════╩═══════════╩═════════════╝

我想创建一个可以接受 regionName 作为参数的函数,然后它将列出该地区的所有国家。

我试过了

create or replace function LISTNATION(regionName IN region.r_name%type)
return varchar2
IS
result varchar2(500)
begin
    for aRow IN (select r_key, r_name, n_name
                 from nation n, region r
                 where n_regionkey = r_Key
                 and r_key = regionKey)
    loop
        result := result || aRow.name || ', ';
    end loop;
    return result;
end LISTNATION;

但这返回了编译错误

为什么会发生此编译错误,是否会通知我们代码的哪一部分导致了这些错误?

谢谢

这里缺少分号:

CREATE OR REPLACE FUNCTION LISTNATION (regionName IN region.r_name%TYPE)
   RETURN VARCHAR2
IS
   result  VARCHAR2 (500);                         --> here
BEGIN
   FOR aRow IN (SELECT r_key, r_name, n_name
                  FROM nation n, region r
                 WHERE     n_regionkey = r_Key
                       AND r_key = regionKey)
   LOOP
      result := result || aRow.name || ', ';
   END LOOP;

   RETURN result;
END LISTNATION;
/

如果出错,请查询USER_ERRORS,例如

SQL> create or replace function LISTNATION(regionName IN region.r_name%type)
  2  return varchar2
  3  IS
  4  result varchar2(500)
  5  begin
  6      for aRow IN (select r_key, r_name, n_name
  7                   from nation n, region r
  8                   where n_regionkey = r_Key
  9                   and r_key = regionKey)
 10      loop
 11          result := result || aRow.name || ', ';
 12      end loop;
 13      return result;
 14  end LISTNATION;
 15  /

Warning: Function created with compilation errors.

SQL> select * from user_errors where name = 'LISTNATION';

NAME                           TYPE           SEQUENCE       LINE   POSITION
------------------------------ ------------ ---------- ---------- ----------
TEXT
--------------------------------------------------------------------------------
ATTRIBUTE MESSAGE_NUMBER
--------- --------------
LISTNATION                     FUNCTION              1          5          1
PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following:

   := ; not null default character
The symbol ";" was substituted for "BEGIN" to continue.
ERROR                103


SQL>

或者,如果您使用的是 SQL*Plus,show errors 也一样(只是更漂亮):

SQL> show err
Errors for FUNCTION LISTNATION:

LINE/COL ERROR
-------- -----------------------------------------------------------------
5/1      PLS-00103: Encountered the symbol "BEGIN" when expecting one of
         the following:
         := ; not null default character
         The symbol ";" was substituted for "BEGIN" to continue.

SQL>