迭代 PL/SQL 中的列

发布于 2024-10-12 20:02:41 字数 459 浏览 2 评论 0原文

我有一个包含 EmpID、Empname、Salary 的表 Emp,我正在尝试为每个员工进行计算。但我在尝试迭代每个 emp 进行计算时遇到问题。但我不能使用显式游标。

所以现在我只是想创建 empID 列表:

Declare
    aRows Number;
    eid emp_ID%TYPE;
Begin
    Select Count(*)
    Into aRows 
    from emp;

    Select emp_ID
    Into eid 
    From emp;

    FOR days IN 1..Tot_Rows
    Loop
        Dbms_Output.Put_Line(eid);
        eid := eid + 1;
    End Loop;
END; 

但我收到错误: PLS-00320: 该表达式的类型声明不完整或格式错误

I have a table Emp with EmpID, Empname, Salary and I am trying to do a calculation for each employee. But I am having problems trying to iterate over each emp to do the calculation. I cant use explicit cursors though.

So right now I am just trying to create the list of empIDs:

Declare
    aRows Number;
    eid emp_ID%TYPE;
Begin
    Select Count(*)
    Into aRows 
    from emp;

    Select emp_ID
    Into eid 
    From emp;

    FOR days IN 1..Tot_Rows
    Loop
        Dbms_Output.Put_Line(eid);
        eid := eid + 1;
    End Loop;
END; 

But I get the error:
PLS-00320: the declaration of the type of this expression is incomplete or malformed

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

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

发布评论

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

评论(2

£噩梦荏苒 2024-10-19 20:02:41

在 PL/SQL 中迭代表中的行的最简单方法是执行类似的操作。

BEGIN
  FOR employees IN (SELECT emp_id FROM emp)
  LOOP
    dbms_output.put_line( employees.emp_id );
  END LOOP;
END;

或者,您可以将所有 EID 值提取到 PL/SQL 集合中并迭代该集合,如本示例所示

DECLARE
  TYPE emp_id_tbl IS TABLE OF emp.emp_id%type;
  l_emp_ids emp_id_tbl ;
BEGIN
  SELECT emp_id
    BULK COLLECT INTO l_emp_ids 
    FROM emp;

  FOR i IN l_emp_ids .FIRST .. l_empnos.LAST
  LOOP
    dbms_output.put_line( l_emp_ids (i) );
  END LOOP;
END;

如果您的查询可以返回但是,如果有数千行,将所有数据提取到集合中可能会使用比您想要的更多的 PGA 内存,并且您可能需要使用 LIMIT 子句以块的形式提取行。但目前看来,这似乎有些超前了。

The simplest way to iterate over the rows in a table in PL/SQL is to do something like

BEGIN
  FOR employees IN (SELECT emp_id FROM emp)
  LOOP
    dbms_output.put_line( employees.emp_id );
  END LOOP;
END;

Alternately, you could fetch all the EID values into a PL/SQL collection and iterate over the collection, as in this example

DECLARE
  TYPE emp_id_tbl IS TABLE OF emp.emp_id%type;
  l_emp_ids emp_id_tbl ;
BEGIN
  SELECT emp_id
    BULK COLLECT INTO l_emp_ids 
    FROM emp;

  FOR i IN l_emp_ids .FIRST .. l_empnos.LAST
  LOOP
    dbms_output.put_line( l_emp_ids (i) );
  END LOOP;
END;

If your query can return thousands of rows, however, fetching all the data into the collection may use more of the PGA memory than you'd like and you may need to fetch rows in chunks using the LIMIT clause. But that would seem to be getting ahead of ourselves at this point.

ゞ记忆︶ㄣ 2024-10-19 20:02:41

Justin Cave 已经解释了如何做到这一点,但要具体看看您得到的错误,那是因为:

eid emp_ID%TYPE;

当使用 %TYPE 时,您必须指定表名以及列名:

eid emp.emp_ID%TYPE;

如果您选择了行中的所有列,您还可以查看%ROWTYPE

您的方法还做出了两个假设:对 eid 的初始选择找到了最低的 ID,这绝不是保证的;并且所有后续 ID 值都是连续的。您正在声明并填充 aRows,但引用 Tot_Rows

Justin Cave has explained how to do it, but to specifically look at the error you got, that was because of this:

eid emp_ID%TYPE;

When using the %TYPE you have to specify the table name as well as the column name:

eid emp.emp_ID%TYPE;

If you were selecting all the columns in the row you could also look at %ROWTYPE.

Your approach was also making two assumptions: that the initial select into eid found the lowest ID, which is by no means guaranteed; and that all the subsequent ID values are sequential. And you're declaring and populating aRows but referring to Tot_Rows.

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