Oracle PL SQL 函数没有 INTO 子句,但它实际上有一个 SELECT INTO

Oracle PL SQL function has no INTO clause, but it actually has a SELECT INTO

我在包中有一个函数。

body:

CREATE OR REPLACE PACKAGE BODY pkg_developers AS

     FUNCTION lookup_developer_studio(p_studio_name IN DEVELOPERSTUDIOS.STUDIONAME%type)
            RETURN INTEGER
        IS
            f_developerstudioid INTEGER;
        BEGIN
            SELECT developerstudioid INTO f_developerstudioid
            FROM developerStudios
            WHERE studioName = p_studio_name;
            RETURN (f_developerstudioid);
        END lookup_developer_studio;
END pkg_developers;

header:

CREATE OR REPLACE PACKAGE pkg_developers
AS

    FUNCTION lookup_developer_studio(p_studio_name IN DEVELOPERSTUDIOS.STUDIONAME%type) RETURN INTEGER;
END pkg_developers;

我正在像这样测试功能:

BEGIN
    select pkg_developers.lookup_developer_studio('some name') from dual;
END;

我得到的错误:

[2022-04-16 13:06:06] [65000][6550]
[2022-04-16 13:06:06]   ORA-06550: line 6, column 5:
[2022-04-16 13:06:06]   PLS-00428: an INTO clause is expected in this SELECT statement
[2022-04-16 13:06:06] Summary: 1 of 1 statements executed, 1 failed in 15 ms (285 symbols in file)

但据我所知,我确实有一个 INTO 子句?

当我 运行 静态查询时: SELECT developerstudioid FROM developerStudios WHERE studioName = 'some name'; 我确实得到 1 个整数结果。

好吧,您用来测试的代码与您最初发布的代码不一样 - 这实际上是 PL/SQL 块,然后需要一个 INTO 子句:

SQL> BEGIN
  2      select pkg_developers.lookup_developer_studio('some name') from dual;
  3  END;
  4  /
    select pkg_developers.lookup_developer_studio('some name') from dual;
    *
ERROR at line 2:
ORA-06550: line 2, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement

如果它只是 SELECT(在 SQL 水平)那么是的 - 你不需要 INTO:

SQL> select * from developerstudios;

STU DEVELOPERSTUDIOID
--- -----------------
MGM               100

SQL>     select pkg_developers.lookup_developer_studio('MGM') from dual;

PKG_DEVELOPERS.LOOKUP_DEVELOPER_STUDIO('MGM')
---------------------------------------------
                                          100

SQL>

如果您想要 PL/SQL,则声明一个将存储该值的变量:

SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
  2    l_id developerstudios.developerstudioid%type;
  3  BEGIN
  4      select pkg_developers.lookup_developer_studio('MGM') INTO l_id from dual;
  5      dbms_output.put_line('Result = ' || l_id);
  6  END;
  7  /
Result = 100

PL/SQL procedure successfully completed.

SQL>