【发布时间】:2019-08-12 09:59:24
【问题描述】:
我有多个 id:
Select count(*) Into count_id From table_I;--4
我知道我总共有total_user = 109(记录数)。所以我想把它分成相等的组:
Select round(count(*)/count_user,0) Into mapUser From table_U;
所以我有4 group。在first three will be 27 和last should be 28 用户中。
现在我想for each group 分配唯一ID。
set serveroutput on
declare
CURSOR cur IS Select * From table_U FOR UPDATE OF USER_ID;
mapUser NUMBER;
l_rec table_U%rowtype;
x_mapUser Number := 0;--number between 0-27
c_obj_id NUMBER := 1;
count_id NUMBER := 0;
type T1 is table of number(10) index by binary_integer;
V1 T1;
begin
Select count(*) Into count_id From table_I;--count_id = 4
Select round(count(*)/count_id,0) Into mapUser From table_U; --mapUser = 27
SELECT id BULK COLLECT INTO V1 FROM table_I;--it's 4 id (id_1, id_2, id_3, id_4)
OPEN cur;
LOOP FETCH cur INTO l_rec;
EXIT WHEN cur%notfound;
IF x_mapUser > mapUser Then --0-27 > 27
x_mapUser := 1;
c_obj_id := c_obj_id +1;--next value from V1
End if;
UPDATE table_U SET USER_ID = V1(c_obj_id) WHERE CURRENT OF cur;
x_mapUser := x_mapUser +1;
END LOOP;
CLOSE cur;
end;
但我不知道如何更改我的IF 和cur 的最后一个值分配以及id_4。我在这里做错了:/
【问题讨论】:
-
样本数据和期望的结果真的很有帮助。
标签: sql oracle plsql cursor bulk