简体   繁体   English

'select * from [table_name]'是一个游标吗?

[英]'select * from [table_name]' is secretly a cursor?

A few days back I was asked during an interview if select * from [table_name] is a cursor since it fetches a number of records at a time? 几天前,我在一次采访中被问及select * from [table_name]中的select * from [table_name]是否是游标,因为它一次获取多个记录?

If yes then of which type? 如果是,那是哪种类型? Explicit or Implicit and why ? 显式还是隐式 ,为什么?

Please, if anyone can elaborate on this it would be a great help. 请,如果任何人都可以对此进行详细说明,那将是很大的帮助。

Oracle internally creates a cursor to serve your select query hence we call it IMPLICIT cursor. Oracle在内部创建一个游标以服务您的选择查询,因此我们将其称为IMPLICIT游标。

Whereas we call the following an explicit cursor which you create on your own to have more controls on it. 而我们将以下内容称为显式游标,您可以自行创建该游标以对其进行更多控制。

CURSOR cur IS SELECT col FROM table WHERE condition;

The explicit cursors are those whcih explicitly are opened, fetched data, closed. 显式游标是显式打开,获取数据,关闭的游标。

As stated, the usage of cursor have 2 parts: 如前所述,游标的用法分为两部分:

  1. definition of the cursor 光标的定义
  2. usage of cursor 游标的用法

Definition of the cursor can be as 游标的定义可以是

CURSOR c IS SELECT col1, col2 FROM table_name 游标c是SELECT col1,col2 FROM table_name

or 要么

FOR i IN (SELECT col1, col2 FROM table_name) 对于我(从表名称中选择col1,col2)

The differences comes next: 接下来是差异:

  1. The explicit cursor needs 显式游标需要

OPEN, FETCH, EXIT WHEN, CLOSE 打开,获取,退出时间,关闭

  1. The implicit cursor performs all above steps in the FOR LOOP statement. 隐式游标在FOR LOOP语句中执行上述所有步骤。

Concluding, a SELECT statement is an implicit cursor. 最后,SELECT语句是一个隐式游标。

Best, Mikcutu. 最好,Mikcutu。

Yes, every query represents a cursor. 是的,每个查询都代表一个游标。

The Concepts Guide links from cursor to private SQL area , which it defines as 概念指南从光标链接到专用SQL区域 ,该区域定义为

An area in memory that holds a parsed statement and other information for processing. 内存中包含已解析语句和其他信息以供处理的区域。 The private SQL area contains data such as bind variable values, query execution state information, and query execution work areas. 专用SQL区域包含诸如绑定变量值,查询执行状态信息和查询执行工作区之类的数据。

The PL/SQL Language Reference says: PL / SQL语言参考说:

An implicit cursor is a session cursor that is constructed and managed by PL/SQL. 隐式游标是由PL / SQL构造和管理的会话游标。 PL/SQL opens an implicit cursor every time you run a SELECT or DML statement. 每次您运行SELECT或DML语句时,PL / SQL都会打开一个隐式游标。

and

An explicit cursor is a session cursor that you construct and manage. 显式游标是您构造和管理的会话游标。 You must declare and define an explicit cursor, giving it a name and associating it with a query (typically, the query returns multiple rows). 您必须声明并定义一个显式游标,为其指定一个名称并将其与查询关联(通常,该查询返回多行)。

These are really concepts from PL/SQL, so I am not sure it is meaningful to use them for cursors from other clients such as a SQL*Plus or SQL Developer command line or a Java program. 这些实际上是PL / SQL中的概念,因此我不确定将它们用于其他客户端(例如SQL * Plus或SQL Developer命令行或Java程序)的游标是否有意义。 I'd say a cursor is only "explicit" if you name it in some PL/SQL code using the cursor keyword, and since you didn't (and can't) do that for a query from your command line, it's closer to the PL/SQL concept of an implicit cursor. 我想说的是,如果您使用cursor关键字在某些PL / SQL代码中将其命名,则该游标只是“显式的”,并且由于您没有(也不能)从命令行进行查询,因此更接近隐式游标的PL / SQL概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM