Oracle 中的显式游标和隐式游标有什么区别?

2021-12-24 00:00:00 oracle plsql

我对 PL/SQL 中的游标术语有点生疏.有人知道吗?

I am a bit rusty on my cursor lingo in PL/SQL. Anyone know this?

推荐答案

隐式游标是 Oracle 在您执行查询时自动"为您创建的游标.编码更简单,但遭受

An implicit cursor is one created "automatically" for you by Oracle when you execute a query. It is simpler to code, but suffers from

  • 效率低下(ANSI 标准规定它必须获取两次以检查是否有多个记录)
  • 数据错误的脆弱性(如果你得到两行,它会引发一个 TOO_MANY_ROWS 异常)

示例

SELECT col INTO var FROM table WHERE something;

显式游标是您自己创建的游标.它需要更多代码,但提供更多控制权 - 例如,如果您只想要第一条记录并且不关心是否有其他记录,则可以只打开-获取-关闭.

An explicit cursor is one you create yourself. It takes more code, but gives more control - for example, you can just open-fetch-close if you only want the first record and don't care if there are others.

示例

DECLARE   
  CURSOR cur IS SELECT col FROM table WHERE something; 
BEGIN
  OPEN cur;
  FETCH cur INTO var;
  CLOSE cur;
END;

相关文章