簡體   English   中英

pl sql用同一表中的值更新行

[英]pl sql update rows with value from the same table

在座席表中,我的字段bossId是其他座席的ID,因此我為座席表中的所有其他字段生成了數據,現在我需要為所有行設置bossId。

因此,每一行都需要從同一張表中為其bossId字段選擇agentId。 可能有一些特工同一個老板,但特工不能是他自己的老板。

這是我的桌子

+---------+------+--------+
| agentId | name | bossId |
+---------+------+--------+
|     123 | aaa  |        |
|     124 | bbb  |        |
|     125 | ccc  |        |
|     126 | ddd  |        |
+---------+------+--------+

想要的轉售:

+---------+------+--------+
| agentId | name | bossId |
+---------+------+--------+
|     123 | aaa  |    124 |
|     124 | bbb  |    123 |
|     125 | ccc  |    126 |
|     126 | ddd  |    123 |
+---------+------+--------+

所以空列bossId需要用同一表的AgentId填充如何在pl sql中做到這一點?

更新:嘗試使用此代碼似乎沒問題,但出現錯誤

begin
  for i in 1..17 loop
    update policeman p
    set bossid = (select p2.officerid
                 from policeman p2
                 order by dbms_random.value
                 where rownum = 1)
                 where rownum =i;
    end loop;
    end ;

錯誤:

ORA-06550: line 6, column 18:
PL/SQL: ORA-00907: missing right parenthesis
ORA-06550: line 3, column 5:
PL/SQL: SQL Statement ignored

這是您想要的嗎?

update t
    set bossid = (select max(bossid) keep (dense_rank first order by dbms_random.random)
                  from t
                 );

注意:如果您的數據不小,這可能不是很有效。

只要agentid是唯一的,這應該可以工作。

update t tgt set bossid = (select max(agentid) from t src where tgt.agentid<>src.agentid);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM