简体   繁体   English

如何在oracle存储过程中返回自己的类型数据?

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

I create a type data in oracle for a procedure. 我在oracle中为过程创建类型数据。

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? 您是否真的要生成一个集合,该集合具有与usarios表相同的行数,并且该集合的每个元素中的对象值都相同? 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 12c数据库中运行了DDL,将古老的EMP表替换为您的usuario表,它似乎可以正常工作。

Oracle EMP and DEPT tables Oracle EMP和DEPT表

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 . 我注意到的唯一潜在错误是您在查询中两次from usuario中包含from usuario一次:一次是针对多集的,一次是作为外部SELECT的源的。 I substituted from dual for from usuario at the end of the query. 在查询结束时,我用from dual替换了from usuario

I removed the WHERE clause for convenience; 为了方便起见,我删除了WHERE子句。 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). 确保NESTED_FILA_USUARIO类型由相同的模式所拥有的outnested过程,或者那么你将有包括模式名称前缀,并确保有来自所有者的直接NESTED_FILA_USUARIO到的模式outnested (角色不要”在已编译的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 7839,KING,总裁

And that's expected for this query. 这是该查询所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Oracle存储过程可以返回Java中可用的对象吗? - Can an Oracle stored procedure return an object useable in Java? 如何使用JPA和Spring Data执行存储过程? - How can I execute a stored procedure with JPA & Spring Data? 如何让子类返回自己的类型? - How can I get subclasses to return their own type? JPA和PostgreSQL:如何使用void返回类型调用存储过程? - JPA & PostgreSQL: How do I call a stored procedure with void return type? 如何使用JDBC / Spring调用Oracle存储过程,其中一些参数类型是用户定义的? - How can I call an Oracle stored procedure with JDBC/Spring where some of the parameter types are user defined? Java:如何从Oracle打印存储过程数据 - Java: How to print stored procedure data from Oracle Spring JdbcTemplate可以等待oracle存储过程完成多长时间 - how long can Spring JdbcTemplate wait for an oracle stored procedure to finish 将oracle对象类型传递给j​​ava存储过程 - Pass oracle object type to java stored procedure Oracle DBMS可以从Java存储过程调用返回Java对象吗? - Can Oracle DBMS return a Java object from a Java stored procedure call? 我在 Oracle 数据库中有一个 SQL 存储过程,参数为时间戳。 如何调用程序 - I have a SQL Stored procedure in Oracle database with parameter as timestamp. How to call the procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM