2

我在 PL/SQL 中有以下存储过程:

CREATE OR REPLACE PROCEDURE sample_procedure AS 
DECLARE 
    TYPE list_of_names_t 
      IS TABLE OF emp.emp_index%TYPE; 
    ignoreIndexes LIST_OF_NAMES_T := List_of_names_t();
BEGIN
-- Logic here which fills the values in the collection ignoreIndexes   
END;

从外部调用此存储过程时,如下所示:

    SET SERVEROUTPUT ON
    EXEC sample_procedure
    -- Line YY

@ Line YY,我想从ignoreindexes存储过程中准备的集合中没有索引的 emp 表中检索记录。

1)如何将ignoreindexes存储过程中创建的嵌套表返回给外界,以便我可以使用该表中的索引

提前致谢

4

1 回答 1

8

首先,它们的类型需要在过程外部声明,以便类型定义对过程外部的代码可见。您可以在 SQL 中声明类型

CREATE TYPE list_of_names_t
    AS TABLE OF NUMBER;

或者你可以在 PL/SQL 中声明它

CREATE OR REPLACE PACKAGE types_package
AS
  TYPE list_of_names_t
    IS TABLE OF emp.emp_index%type;
END;

然后,您的过程将不得不使用并返回 SQL 类型

CREATE OR REPLACE PROCEDURE sample_procedure( 
  p_ignore_indexes OUT list_of_names_t 
) 
AS 
BEGIN
  -- Logic here which fills the values in the collection p_ignore_indexes 
END;

或 PL/SQL 类型

CREATE OR REPLACE PROCEDURE sample_procedure( 
   p_ignore_indexes OUT types_package.list_of_names_t 
) 
AS 
BEGIN
  -- Logic here which fills the values in the collection p_ignore_indexes 
END;

当然,如果您的代码的目的是返回一个集合,那么编写函数比编写过程更有意义

CREATE OR REPLACE FUNCTION sample_function
  RETURN types_package.list_of_names_t
AS 
  ignore_indexes types_package.list_of_names_t;
BEGIN
  -- Logic here which fills the values in the collection ignore_indexes 
  RETURN ignore_indexes;
END;

当您调用该过程时,您会执行类似的操作

DECLARE
  l_ignore_indexes types_package.list_of_names_t;
BEGIN
  l_ignore_indexes := sample_function; 
  -- Do something with l_ignore_indexes
END;

或者

DECLARE
  l_ignore_indexes types_package.list_of_names_t;
BEGIN
  sample_procedure( l_ignore_indexes );
  -- Do something with l_ignore_indexes
END;
于 2013-01-10T00:47:47.830 回答