Search code examples
oracle-databaseplsqldatagrip

Oracle PL/SQL SELECT INTO clause thinks it needs another INTO


I have a simple test function where I'm passing in a specific ID (the primary key of the table I'm selecting from), and computing a simple function on it and the parameters.

The skeleton code and test:

create or replace function test(id varchar2, area float) return float is
    theRow forest%ROWTYPE;
begin
    select * into theRow from forest where Forest_No = id;
    return area / theRow.Area;
end;

begin
    select test('1', 16000) from dual;
end;

The output:

[2019-10-14 21:19:10] [65000][6550] ORA-06550: line 2, column 5:
[2019-10-14 21:19:10] PLS-00428: an INTO clause is expected in this SELECT statement

I am at a loss for what to do here, as far as I can tell the documentation and examples use the same order and syntax. I have tried moving the into clause to the end as in Postgresql, but that did not work.

What have I missed here?


Solution

  • Issue is in calling statement.

    Whenever select statement is used in plsql block it must have into clause to assign return value to variable.

    You should remove begin and end from your calling code:

    --begin -- remove this
        select test('1', 16000) from dual;
    --end; -- remove this
    

    Or if you want to use it in plsql block then add into clause:

    Declare
    Area_ float(precision);
    begin
        select test('1', 16000) into area_ from dual;
        -- use area_ in your code wherever required
        dbms_output.put_line('area: ' || area_);
    end;
    

    Cheers!!