Oracle PL/SQL:如何从游标创建类型

Oracle PL/SQL:如何从游标创建类型,oracle,types,plsql,record,Oracle,Types,Plsql,Record,我可以从游标定义类型吗 目前,为了从一些表字段创建记录变量,我必须编写以下代码: declare cursor cur is select f_1, f_2, f_3, f_4 from mytable where 1=0; myvar cur%rowtype;

我可以从游标定义类型吗

目前,为了从一些表字段创建记录变量,我必须编写以下代码:

declare

    cursor cur
    is
        select 
            f_1,
            f_2,
            f_3,
            f_4
        from
            mytable
        where 
            1=0;

    myvar cur%rowtype;              -- use the cursor to declare myvar

begin
    null;
end;
我想这样写:

declare

    cursor cur
    is
        select 
            f_1,
            f_2,
            f_3,
            f_4
        from
            mytable
        where 
            1=0;

    type mytype is cur%rowtype;     -- declare "mytype" from cursor
    myvar mytype;                   -- use "mytype" to declare "myvar"

begin
    null;
end;
在这个简单的例子中,这看起来不太有用,但在实际问题中可能有用

另一种方法是手动创建记录类型:

declare

    type mytype is record           -- declare "mytype"
    (
        f_1    mytable.f_1%type,
        f_2    mytable.f_2%type,
        f_3    mytable.f_3%type,
        f_4    mytable.f_4%type
    );
    myvar mytype;                   -- use "mytype" to declare "myvar"

begin
    null;
end;
但它对我来说更脏(我必须将每个字段名重复两次,表名重复多次)

我可以从游标定义类型吗

是的,您可以使用
子类型
关键字(而不是
类型
关键字)定义自己的类型,该类型基于
游标名称%rowtype
记录类型(在这种情况下基本上是同义词)

以下是一个例子:

set serveroutput on;
declare
  cursor c1 is
    select 1 as col1
         , 2 as col2
         , 3 as col3
     from dual;

  subtype mytype is c1%rowtype;
  l_myvar mytype;
begin
  open c1;
  fetch c1 into l_myvar;
  dbms_output.put(to_char(l_myvar.col1) || ' : ');
  dbms_output.put(to_char(l_myvar.col2) || ' : ');
  dbms_output.put_line(to_char(l_myvar.col3));
  close c1;
end;
结果:

anonymous block completed

1 : 2 : 3

我认为您在这里要做的是使用ROWTYPE变量,如下所示:

DECLARE
  CURSOR c1 IS
    SELECT t.COL1, t.COL2, t.COL3
      FROM MY_TABLE t
      WHERE t.ADD_DATE >= TRUNC(SYSDATE, 'MONTH');

  c1Row  c1%ROWTYPE;
BEGIN
  OPEN c1;

  LOOP
    FETCH c1 INTO c1Row;
    EXIT WHEN c1%NOTFOUND;

    DBMS_OUTPUT.PUT_LINE('COL1=' || c1Row.COL1 ||
                         '  COL2=' || c1Row.COL2 ||
                         '  COL3=' || c1Row.COL3);
  END LOOP;

  CLOSE c1;
END;

分享和享受