简体   繁体   中英

How can I return a own type data in oracle stored procedure?

I create a type data in oracle for a procedure.

procedure:

create or replace TYPE ROW_USER as object 
(
    ID                  NUMBER(4),
    ROLES_ID            NUMBER(1),
    FIRST_NAME          VARCHAR2(60 BYTE),
    SECOND_NAME         VARCHAR2(60 BYTE),
    FIRST_LAST_NAME     VARCHAR2(60 BYTE),
    SECOND_LAST_NAME    VARCHAR2(60 BYTE),
    CC                  NUMBER(10,0),
    EMAIL               VARCHAR2(60 BYTE)
);

and:

create or replace type NESTED_ROW_USER as table of ROW_USER;

when I use this type data in an anonymous blockand it works.

set serveroutput on; 
DECLARE  
    ROWUSR NESTED_ROW_USER;
BEGIN
    select cast( multiset(select id, ROLES_ID, FIRST_NAME, SEGUNDO_NOMBRE, 
    FIRST_LAST_NAME, SECOND_LAST_NAME, CC, EMAIL from USERS where id =  1) as 
    NESTED_ROW_USER)
       into ROWUSR
    from users;  

DBMS_OUTPUT.PUT_LINE(ROWUSR(1).id||','||ROWUSR(1).ROLES_ID||','|| 
    ROWUSR(1).FIRST_NAME);
    END;

block result:

Process PL/SQL exited successfully.
1,1,DIEGO

But when I use it in a stored procedure to return it, I get an error.

Procedure:

create or replace PROCEDURE outnested(ROWUSR OUT NESTED_ROW_USR)
AS
BEGIN
  select cast( multiset(select id, ROLES_ID, FIRST_NAME, SECOND_NAME, 
  FIRST_LAST_NAME, SECOND_LAST_NAME, CC, EMAIL from USERS where id =  1) as 
  NESTED_ROW_USER)
    into ROWUSR
    from users;     
END;

error:

ORA-06550: line 5, column 16:
PLS-00302: component 'NESTED_ROW_USER' must be declared
ORA-06550: line 5, column 11:
PL/SQL: Item ignored
ORA-06550: line 14, column 16:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 10, column 3:
PL/SQL: Statement ignored

Why does it not return the value?

Are you really trying to generate a collection that has the same number of rows as the usarios table with the same object values in every element of the collection? That seems odd.

My guess is that you really want something like this

create or replace PROCEDURE outnested(p_FILAUSU OUT NESTED_FILA_USUARIO)
AS
BEGIN
  select FILA_USUARIOr( id, 
                        ROLES_ID, 
                        PRIMER_NOMBRE, 
                        SEGUNDO_NOMBRE, 
                        PRIMER_APELLIDO, 
                        SEGUNDO_APELLIDO, 
                        CC, 
                        EMAIL )
    bulk collect into p_FILAUSU 
    from USUARIOS 
   where id =  1;
END;

I ran your DDL in an Oracle 12c database, substituting the venerable EMP table for your usuario table, and it seemed to work.

Oracle EMP and DEPT tables

The only potential error I noted was that you included from usuario twice in your query: once for the multiset and once as the source of the outer SELECT . I substituted from dual for from usuario at the end of the query.

I removed the WHERE clause for convenience; I'm assuming you would retain it.

Make sure that the NESTED_FILA_USUARIO type is owned by the same schema as the outnested procedure, or you will then have to include the schema name prefix and make sure there is a direct grant from the owner of NESTED_FILA_USUARIO to the schema of outnested (roles don't work within compiled PL/SQL).

create or replace TYPE FILA_USUARIO as object 
(
  EMPNO NUMBER(4),
  ENAME VARCHAR2(10),
  JOB   VARCHAR2(9),
  MGR   NUMBER(4),
  HIREDATE DATE,
  SAL NUMBER(7,2),
  COMM NUMBER(7,2),
  DEPTNO NUMBER(2)
);
create or replace type NESTED_FILA_USUARIO as table of FILA_USUARIO;

Here is the anonymous block:

DECLARE  
    FILAUSU NESTED_FILA_USUARIO;
BEGIN
    select cast( multiset(select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from EMP) as 
    NESTED_FILA_USUARIO)
       into FILAUSU
    from dual;  

DBMS_OUTPUT.PUT_LINE(FILAUSU(1).EMPNO||','||FILAUSU(1).ENAME||','|| 
    FILAUSU(1).JOB);
END;

Here is the stored procedure, with a test:

create or replace PROCEDURE outnested(FILAUSU OUT NESTED_FILA_USUARIO)
AS
BEGIN
  select cast( multiset(select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from EMP) as 
  NESTED_FILA_USUARIO)
    into FILAUSU
    from dual;     
END;
/
declare
  filausu NESTED_FILA_USUARIO;
begin
  outnested(filausu);
  DBMS_OUTPUT.PUT_LINE(FILAUSU(1).EMPNO||','||FILAUSU(1).ENAME||','|| 
    FILAUSU(1).JOB);
end;
/

The output was this:

7839,KING,PRESIDENT

And that's expected for this query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM