ORA-00900: invalid SQL statement- when run a package in oracle 12c

1k Views Asked by At

I am using Oracle 12c database and trying to run a package using SQL commands.

CREATE OR REPLACE PACKAGE "PK_CP_OTM" as
FUNCTION F_CP_OPTIMIZATION (
     v_current_day IN VARCHAR2,
     v_branch_code IN VARCHAR2)
     RETURN VARCHAR2;
END PK_CP_OTM;

When I try to execute it using:

DECLARE
BEGIN
EXECUTE IMMEDIATE PK_CP_OTM.F_CP_OPTIMIZATION('20190409','BRNCD001');
END;

It shows:

ORA-00900: invalid SQL statement
ORA-06512: at line 3
00900. 00000 -  "invalid SQL statement"

Thanks for your help.

2

There are 2 best solutions below

1
On BEST ANSWER

As @Littlefoot said, you don't need dynamic SQL here, you can make a static call; but as you are calling a function you do need somewhere to put the result of the call:

declare
  l_result varchar2(30); -- make it a suitable size 
begin
  l_result := pk_cp_otm.f_cp_optimization('20190409','BRNCD001');
end;
/

In SQL*Plus, SQL Developer and SQLcl you can use the execute client command (which might have caused some confusion) and a bind variable for the result:

var result varchar2(30);

exec :result := pk_cp_otm.f_cp_optimization('20190409','BRNCD001');

print result
1
On

There's nothing dynamic here, so - why would you use dynamic SQL at all?

Anyway: if you insist, then you'll have to select the function into something (e.g. a local variable). Here's an example

First, the package:

SQL> set serveroutput on
SQL>
SQL> create or replace package pk_cp_otm
  2  as
  3     function f_cp_optimization (v_current_day  in varchar2,
  4                                 v_branch_code  in varchar2)
  5        return varchar2;
  6  end pk_cp_otm;
  7  /

Package created.

SQL> create or replace package body pk_cp_otm
  2  as
  3     function f_cp_optimization (v_current_day  in varchar2,
  4                                 v_branch_code  in varchar2)
  5        return varchar2
  6     is
  7     begin
  8        return 'Littlefoot';
  9     end;
 10  end pk_cp_otm;
 11  /

Package body created.

How to call the function?

SQL> declare
  2     l_result  varchar2 (20);
  3  begin
  4     execute immediate
  5        'select pk_cp_otm.f_cp_optimization (''1'', ''2'') from dual'
  6        into l_result;
  7
  8     dbms_output.put_line ('result = ' || l_result);
  9  end;
 10  /
result = Littlefoot

PL/SQL procedure successfully completed.

SQL>