【发布时间】:2014-08-22 13:05:55
【问题描述】:
我可以从光标定义类型吗?
目前我必须编写以下内容才能从某些表字段创建记录变量:
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;
但这对我来说更脏(我必须重复每个字段名两次,并且多次重复表名)。
【问题讨论】: