pl/sql循环记录select oracle plsql

发布于 2025-01-02 10:56:41 字数 1046 浏览 1 评论 0原文

我有一个 select 语句,我试图循环并根据 select 语句的条件递增一个变量,然后将该变量作为 out 返回,这样我就可以在某些前端用它做一些事情代码。我正在使用 oracle 11g,我看到了几种可以做到这一点的方法......但我不确定哪种方法是最好的方法。我在下面尝试做一些事情,但由于混乱而再次停止。

首先,我设置我的过程和“变量”,

PROCEDURE SEEKER (pMonkeyID IN Number, vMarkCounter OUT Number)
AS
BEGIN

CURSOR seeker_cur IS
    Select Mokney_approved, Monkey_vaulted 
    from MonkeyBookApps 
    where MonkeyID = pMonkeyID
    and Monkey_doc_type = 'BANANA' 
    order by docu_approv_timestamp,monkey_doc_type,monkey_doc_approved desc

OPEN seeker_cur;

begin

   OPEN Seeker_cur;
   vMarkCounter := 0;

这是我不确定的部分。如果不满足条件,我应该循环然后退出,还是应该执行 if 语句并以某种方式确定是否存在可能大于 1 的记录?如果是这样,那将如何运作?采取一种方式比另一种方式有好处吗?所以...我将对我想要做的事情进行 sudo 编码(如下):

FOR (however many records) in Seeker_cur
IF seeker_cur (not found) or (returns no records)

EXIT or (break for loop);
ELSE
LOOP

vMarkCounter := vMarkCounter + 1;

EXIT WHEN seeker_cur is out of records (somehow)
END IF;
END LOOP;

END; 
END SEEKER;

我确信有几种方法可以做到这一点。你会建议什么方法?

I have a select statement that I am trying to loop over and increment a variable based on the condition of the select statement, then return the variable as an out so I can do something with it in some front end code. I am using oracle 11g and I am seeing a few ways I can do this... but I am not sure which is the best way. I have some of what I am trying to do below, but again stopped because of confusion.

First I am setting my proc and 'in variable'

PROCEDURE SEEKER (pMonkeyID IN Number, vMarkCounter OUT Number)
AS
BEGIN

CURSOR seeker_cur IS
    Select Mokney_approved, Monkey_vaulted 
    from MonkeyBookApps 
    where MonkeyID = pMonkeyID
    and Monkey_doc_type = 'BANANA' 
    order by docu_approv_timestamp,monkey_doc_type,monkey_doc_approved desc

OPEN seeker_cur;

begin

   OPEN Seeker_cur;
   vMarkCounter := 0;

Here is the part I am not sure about. Should I loop and then exit if the condition is not met or should I do an if statement and somehow determine if there is a record that could be greater than one? If so how would that work? Is there a benefit to doing one way over the other? So... I am going to sudo-code what I am trying to do (below):

FOR (however many records) in Seeker_cur
IF seeker_cur (not found) or (returns no records)

EXIT or (break for loop);
ELSE
LOOP

vMarkCounter := vMarkCounter + 1;

EXIT WHEN seeker_cur is out of records (somehow)
END IF;
END LOOP;

END; 
END SEEKER;

I am sure there are a few ways to do this. What ways would you suggest?

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

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

发布评论

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

评论(2

盗琴音 2025-01-09 10:56:41

为什么不使用隐式游标,它会自行打开和关闭:

DECLARE

CURSOR seeker_cur IS
    Select Mokney_approved, Monkey_vaulted 
    from MonkeyBookApps 
    where MonkeyID = pMonkeyID
    and Monkey_doc_type = 'BANANA' 
    order by docu_approv_timestamp,monkey_doc_type,monkey_doc_approved desc;

  vMarkCounter number:=0;

BEGIN

  FOR i IN seeker_cur 
  LOOP
    vMarkCounter := vMarkCounter+1;
  END LOOP;

  dbms_output.put_line(vMarkCounter);
END;

why dont you use implicit cursor , it will open and close itself:

DECLARE

CURSOR seeker_cur IS
    Select Mokney_approved, Monkey_vaulted 
    from MonkeyBookApps 
    where MonkeyID = pMonkeyID
    and Monkey_doc_type = 'BANANA' 
    order by docu_approv_timestamp,monkey_doc_type,monkey_doc_approved desc;

  vMarkCounter number:=0;

BEGIN

  FOR i IN seeker_cur 
  LOOP
    vMarkCounter := vMarkCounter+1;
  END LOOP;

  dbms_output.put_line(vMarkCounter);
END;
吐个泡泡 2025-01-09 10:56:41

在我看来,您的问题的解决方案可能就像这样简单:

SELECT COUNT(*)
INTO   l_some_local_variable
FROM   monkey_book_apps 
WHERE  monkey_id = p_monkey_id
   AND monkey_doc_type = 'BANANA';

RETURN l_some_local_variable;

避免 PL/SQL 循环并使用尽可能简单的 SQL 是(几乎总是)最有效的方法。 Tom Kyte 将循环的逐行执行称为“缓慢”。

It seems to me that the solution your problem might be as simple as this:

SELECT COUNT(*)
INTO   l_some_local_variable
FROM   monkey_book_apps 
WHERE  monkey_id = p_monkey_id
   AND monkey_doc_type = 'BANANA';

RETURN l_some_local_variable;

Avoiding PL/SQL loops and using the simplest SQL possible is (almost always) the most efficient way. Tom Kyte calls the row-by-row execution of LOOPs "slow-by-slow".

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