简体   繁体   English

为什么在PL / SQl ORACLE中使用游标?

[英]Why using cursors in PL/SQl ORACLE?

I'm a beginner at PL/SQL, and during studying the course I saw CURSOR and I want to know why we should use it, and when? 我是PL / SQL的初学者,在学习课程期间,我看到了CURSOR ,我想知道为什么要使用它,何时使用? thank you very much 非常感谢你

When you do a SELECT and it returns more than one row you can't save the rows in a variable , so you'll have to use a CURSOR . 当执行SELECT并返回多行时,您不能将行保存在变量中 ,因此必须使用CURSOR If you are familiar with programming a CURSOR is something like an Array . 如果您熟悉编程,则CURSOR类似于Array So if you do a SELECT and save the results in a variable like in the code: 因此,如果执行SELECT并将结果保存在代码中的变量中,例如:

SELECT id INTO v_id FROM table;

and if more than one row is returned, you cant save the rows in the variable v_id , and a TOO_MANY_ROWS Exception will be thrown. 如果返回多行,则无法将行保存在变量v_id中 ,并且将引发TOO_MANY_ROWS异常。 Reference: http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23plsql-1906474.html 参考: http : //www.oracle.com/technetwork/issue-archive/2013/13-mar/o23plsql-1906474.html

Also, if you've seen Oracle's FOR ... IN (SELECT ...) ... LOOP ... END LOOP statement, that's using an implicit cursor. 另外,如果您看过Oracle的FOR ... IN(SELECT ...)... LOOP ... END LOOP语句,则使用隐式游标。

The reason to use the explicit cursor method is that you can do more things with the cursor, such as BULK COLLECT which can greatly improve your processing performance in many, but not all, situations. 使用显式游标方法的原因是,您可以使用游标执行更多操作,例如BULK COLLECT,它可以在很多(但不是全部)情况下极大地提高处理性能。 That greater control (beyond just doing BULK COLLECT) is helpful as you develop more-elaborate processes. 在开发更复杂的流程时,更好的控制(不仅仅是执行BULK COLLECT)是有帮助的。

Good luck on your journey into Oracle. 祝您进入Oracle旅途顺利。 I've been using it for 14 years and am a big fan. 我已经使用14年了,并且非常喜欢。

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

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