PL/SQL中的游标对象是什么

发布于 2024-10-08 18:19:40 字数 325 浏览 3 评论 0原文

我理解 PL/SQL 变量的概念,但我无法清楚地了解 PL/SQL 中的游标对象。

我在某处读过下面的文字,感觉自己无法理解光标对象的概念。

“如果光标变量还没有 已分配给任何光标对象, 隐式 OPEN FOR 语句 为变量创建一个对象。
如果在 OPEN 时光标 变量已经指向一个 游标对象,则 OPEN FOR 不会 创建一个新对象。相反,它 重用现有对象并且 将新查询附加到该对象。 光标对象被维护 与游标或查询分开 本身。 ”

请解释一下它的内部工作原理......

I understand the concept of PL/SQL variables but I am unable to get clear picture of cursor objects in PL/SQL in my mind.

I have read the below written text somewhere and and feel myself unable to understand the concept of cursor objects.

"If the cursor variable has not yet
been assigned to any cursor object,
the OPEN FOR statement implicitly
creates an object for the variable.
If at the time of the OPEN the cursor
variable already is pointing to a
cursor object, then OPEN FOR does not
create a new object. Instead, it
reuses the existing object and
attaches a new query to that object.
The cursor object is maintained
separately from the cursor or query
itself. "

please explain me how it works internally.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪眸﹌ 2024-10-15 18:19:40

你可以把它看作C编程语言中使用的指针(如果你学过的话),它指向从SQL语句中检索的每一行数据。将光标移动到下一行以读取下一行。

参考:http://www.cse.unsw。 edu.au/~cs9311/10s2/lectures/week04/PL_SQL.pdf
这是我用来学习PL/SQL的讲义

you can regard it as a pointer which is used in C programming language(if you ever have learnt), it points to each row of the data retrieved from the SQL statement. Move the cursor to next line to read the next row.

Ref: http://www.cse.unsw.edu.au/~cs9311/10s2/lectures/week04/PL_SQL.pdf
this is the lecture notes which I used to learn PL/SQL

冰之心 2024-10-15 18:19:40

据我了解,游标就像指向结果集中的行的指针。在其他编程语言中,它就像一个枚举器。枚举器是一个帮助遍历项目集合的对象。

例如:

declare
  cursor cur is select * from someTable;

begin
  open cur;
  loop
    fetch cur into record;
    exit when cur%notfound;
    dbms_output.put_line('Col1: ' || record.col1 || ', Col2: ' || record.col2);
  end loop;
end;

在 C# 中与此类似:

    string[] collection = new string[]{"A", "B", "C"};
    var enumerator = collection.GetEnumerator();
    while(enumerator.MoveNext())
    {
        Console.WriteLine("current item:" + enumerator.Current);
    }

请注意,使用游标来获取记录数据类似于使用枚举器来获取数据。

希望这对您有帮助。

To my understand, a cursor is like a pointer that point to a row in a result set. In other programming language, it is like a enumerator. A enumerator is a object that helps goes though a collection of items.

For example:

declare
  cursor cur is select * from someTable;

begin
  open cur;
  loop
    fetch cur into record;
    exit when cur%notfound;
    dbms_output.put_line('Col1: ' || record.col1 || ', Col2: ' || record.col2);
  end loop;
end;

would be similar to this in C#:

    string[] collection = new string[]{"A", "B", "C"};
    var enumerator = collection.GetEnumerator();
    while(enumerator.MoveNext())
    {
        Console.WriteLine("current item:" + enumerator.Current);
    }

Notice that you use the cursor to get record data is similar to use a enumerator to get the data.

Hope this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文