zig*_*ggy 0 oracle plsql plsqldeveloper
我正在查看以下两种检索值的方法,并稍后通过insert语句存储它.即通过Pl/SQL游标或直接SQL.两种方法都有任何优势吗?还是有更有效的方法?
方法1
Cursor system_date
Is
select sysdate from dual;
system_date_rec system_date%type;
Open system_Date;
Fetch system_date into system_date_rec;
Insert into table(dateValue)
values(system_date_rec.date);
Run Code Online (Sandbox Code Playgroud)
方法2
dateString varchar(20);
Select sysdate into dateString from dual;
Insert into table(dateValue)
values(dateString);
Run Code Online (Sandbox Code Playgroud)
方法3怎么样:
Insert into table(dateValue)
values(sysdate);
Run Code Online (Sandbox Code Playgroud)
或者假设您确实需要选择以获取数据:
Insert into table(dateValue)
select dateValue from other_table where ...;
Run Code Online (Sandbox Code Playgroud)
关于显式游标或SELECT INTO在需要其中一个或另一个时是否更可取,我会选择SELECT INTO,因为如果您希望查询只返回一行,它会更整洁更安全:
select some_value
into l_var
from other_table
where ...;
if l_var = 'A' then
do_something;
end if;
Run Code Online (Sandbox Code Playgroud)
现在,如果返回的行数不符合预期,您将获得异常(NO_DATA_FOUND或TOO_MANY_ROWS).使用光标,你最终会更改l_var,或者设置为第一个匹配行的值 - 这可能意味着你有一个bug,但不知道它.