迭代 PL/SQL 中的列
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 PL/SQL 中迭代表中的行的最简单方法是执行类似的操作。
或者,您可以将所有 EID 值提取到 PL/SQL 集合中并迭代该集合,如本示例所示
如果您的查询可以返回但是,如果有数千行,将所有数据提取到集合中可能会使用比您想要的更多的 PGA 内存,并且您可能需要使用 LIMIT 子句以块的形式提取行。但目前看来,这似乎有些超前了。
The simplest way to iterate over the rows in a table in PL/SQL is to do something like
Alternately, you could fetch all the EID values into a PL/SQL collection and iterate over the collection, as in this example
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.
Justin Cave 已经解释了如何做到这一点,但要具体看看您得到的错误,那是因为:
当使用
%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:
When using the
%TYPE
you have to specify the table name as well as the column name: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 populatingaRows
but referring toTot_Rows
.