How to return rows from a declare/begin/end block in Oracle?

前端 未结 2 1231
别跟我提以往
别跟我提以往 2020-12-31 04:34

I want to return rows from a select statement within a declare/begin/end block. I can do this in T-SQL but I would like to know how to do it in PL/SQL.

The code loo

2条回答
  •  暖寄归人
    2020-12-31 05:41

    Well, this depends heavily on your data access library.

    You can return any SQL-compatible type as a parameter. This includes complex SQL types and collection types. But most libraries are simply not capable of handling Oracle's object types.

    Either way, my examples will use these object types:

    create type SomeType as object(Field1 VarChar(50));
    
    create type SomeTypeList as table of SomeType;
    

    When your access library can handle object types, you could simply return a list of PL/SQL objects:

    begin
      :list := SomeTypeList(SomeType('a'),SomeType('b'),SomeType('c'));
    end;
    

    If not, you could hack around it by forcing this list into a select and return its result as a cursor:

    declare
      list SomeTypeList;
    begin
      list := SomeTypeList(SomeType('a'),SomeType('b'),SomeType('c'));
      open :yourCursor for
        SELECT A
        FROM   table(list);
    end;
    

提交回复
热议问题