Oracle Replace function

前端 未结 3 1674
死守一世寂寞
死守一世寂寞 2021-01-23 17:09

I need to replace the Table1\'s filed values from Table2\'s values while select query.

Eg:

Table1:

Org                  Permission
--------------         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 17:24

    In Oracle you can run this PL/SQL block and it will do all the work:

    begin
      for x in (
        select permission, descr from table2
      ) loop
        update table1 set permission = replace(permission, x.permission, x.descr);
      end loop;
      commit;
    end
    /
    

    I used this script to create a schema:

    create table table1(
      org varchar2(100),
      permission varchar2(1000)
    );
    
    create table table2(
      permission varchar2(100),
      descr varchar2(1000)
    );
    
    insert into table1(org, permission) values ('Company1', '1,3,7');
    insert into table1(org, permission) values ('Company2', '1,3,8');
    insert into table2(permission, descr) values ('1', 'Read');
    insert into table2(permission, descr) values ('7', 'Execute');
    insert into table2(permission, descr) values ('8', 'Delete');
    

提交回复
热议问题