【发布时间】:2019-06-29 06:48:59
【问题描述】:
我正在尝试从 tableA 循环我的所有记录,并为 columnA = 1 和第二半记录 columnA = 2 设置一半的记录。
declare
Type T2 Is Table Of TableA%Rowtype Index By Binary_Integer;
V2 T2;
Type T3 Is Table Of TableA%Rowtype Index By Binary_Integer;
V3 T3;
Maxrow Number(10);
mHalf Number(10);
begin
Select round(Max(Rownum)/2) Into Maxrow From TableA;
Select * Bulk Collect Into V2 From TableA Where Rownum < Mhalf;
Select * Bulk Collect Into V3 From TableA Where Rownum >= Mhalf;
For I In 1..2 Loop
If I=1 Then
For Z In V2.First..V2.Last Loop
update tableA set columnA = 1 where Rownum = V2(Z);
End Loop;
Elsif I=2 Then
For ZZ In V3.First..V3.Last Loop
update tableA set columnA = 2 where Rownum = V3(ZZ);
End Loop;
End if;
End Loop;
end;
但是有些事情出了问题。当我检查时:
Select Count(*) From tableA Where Rownum > 300;
这里我没有任何记录
【问题讨论】:
-
Where Rownum > 300;永远不会返回任何行。 quote from the manual: "对大于正整数的 ROWNUM 值进行条件测试总是错误的" -
是的,来自@Ted 在 ORCL.Pro 的代码很好
-
但是 PL/SQL 在这里完全是矫枉过正。这可以通过一个简单的 UPDATE 语句来完成
-
那么请说明如何执行更新,谢谢!
-
update table_a set column_a = mod(rownum, 2) + 1
标签: sql oracle loops plsql oracle12c