oracle 在 PL/SQL 中,游标和引用游标有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15355739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In PL/SQL what is a difference between a cursor and a reference cursor?
提问by user2159852
As per my knowledge, a cursor is used to process SQL statements in private area and we can use it further. A Ref cursor is defining a cursor at the spot where it is needed. Please correct me if I am wrong..
据我所知,一个游标用于处理私有区域的SQL语句,我们可以进一步使用它。Ref 游标在需要它的地方定义一个游标。如果我错了请纠正我..
回答by Chris Saxon
A cursor is really any SQL statement that runs DML (select, insert, update, delete) on your database.
游标实际上是在数据库上运行 DML(选择、插入、更新、删除)的任何 SQL 语句。
A ref cursor is a pointer to a result set. This is normally used to open a query on the database server, then leave it up to the client to fetch the result it needs. A ref cursor is also a cursor, though normally ther term cursor is used when discussing static SQL.
引用游标是指向结果集的指针。这通常用于在数据库服务器上打开查询,然后让客户端获取所需的结果。引用游标也是一个游标,但通常在讨论静态 SQL 时使用术语游标。
Ref cursors are typically used to change the where clause of a query, based on user input. For example, this function, either opens a query to the emp
table or the dept
table, depending upon what the user has selected:
引用游标通常用于根据用户输入更改查询的 where 子句。例如,此函数根据用户选择的内容打开对emp
表或dept
表的查询:
create or replace function f (input in varchar2) return sys_refcursor as
cur sys_refcursor;
begin
if input = 'EMP' then
open cur for select * from emp;
elsif input = 'DEPT' then
open cur for select * from dept;
end if;
return cur;
end;
/
回答by Sushmita Mitkar
Static cursor is associated with single SQL query. It is not flexible that is why use dynamic cursor called as ref cursorto use same cursor for different queries.
静态游标与单个 SQL 查询相关联。这就是为什么使用称为引用游标的动态游标来为不同的查询使用相同的游标的原因。