Search code examples
sqloracle-databasestored-proceduresplsqlplsql-package

Error: Cant create procedure with select in PL/SQL


I am creating a procedure in a package and I want to make a simple select but I get this error.

create or replace PACKAGE PK_MAC AS
   /* TODO enter package declarations (types, exceptions, methods etc) here */ 
    PROCEDURE PR_PRUEBAS (
        IDNUM NUMBER := 0, 
        NOMBRES VARCHAR2 := 'Usuario', 
        FECHANACIMIENTO DATE := SYSDATE, 
        ARCHIVOS CLOB := ''
    )
    IS
    BEGIN
        SELECT * FROM MAC;
    END;
END;

Error:

Error(6,3): PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: language "


Solution

  • Package consists of its specification and body. For example:

    SQL> create or replace package pk_mac as
      2    procedure pr_pruebas (p_idnum in number);
      3  end;
      4  /
    
    Package created.
    
    SQL> create or replace package body pk_mac as
      2    procedure pr_pruebas (p_idnum in number)
      3    is
      4      l_ename emp.ename%type;
      5    begin
      6      select ename
      7      into l_ename
      8      from emp
      9      where empno = p_idnum;
     10
     11      dbms_output.put_line(l_ename);
     12    end;
     13  end;
     14  /
    
    Package body created.
    

    Testing:

    SQL> set serveroutput on
    SQL>
    SQL> exec pk_mac.pr_pruebas(7654);
    MARTIN
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    Or, in your case:

    SQL> create or replace PACKAGE PK_MAC AS
      2    PROCEDURE PR_PRUEBAS
      3      (IDNUM NUMBER := 0,
      4       NOMBRES VARCHAR2 := 'Usuario',
      5       FECHANACIMIENTO DATE := SYSDATE,
      6       ARCHIVOS CLOB := '');
      7  end;
      8  /
    
    Package created.
    
    SQL>
    SQL> create or replace PACKAGE body PK_MAC AS
      2    PROCEDURE PR_PRUEBAS
      3      (IDNUM NUMBER := 0,
      4       NOMBRES VARCHAR2 := 'Usuario',
      5       FECHANACIMIENTO DATE := SYSDATE,
      6       ARCHIVOS CLOB := '')
      7    is
      8    begin
      9      null;
     10    end;
     11  end;
     12  /
    
    Package body created.
    
    SQL>
    

    Note that - when you use a select statement in PL/SQL - you have to put the result into something (such as a variable, like I did in my example). You can't just SELECT * FROM MAC ...