Oracle Replace function

前端 未结 3 1677
死守一世寂寞
死守一世寂寞 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:15

    one of the awesome solution provided here to this sort of replace.

    It just created one multiple_replace function:

    CREATE TYPE t_text IS TABLE OF VARCHAR2(256);
    
    CREATE FUNCTION multiple_replace(
      in_text IN VARCHAR2, in_old IN t_text, in_new IN t_text
    )
      RETURN VARCHAR2
    AS
      v_result VARCHAR2(32767);
    BEGIN
      IF( in_old.COUNT <> in_new.COUNT ) THEN
        RETURN in_text;
      END IF;
      v_result := in_text;
      FOR i IN 1 .. in_old.COUNT LOOP
        v_result := REPLACE( v_result, in_old(i), in_new(i) );
      END LOOP;
      RETURN v_result;
    END;
    

    and then you could use this query to replace:

    UPDATE Table1
    SET permission_id = multiple_replace(Permission, (select distinct Permission from table2), 
            (select distinct Permission_name from table2));
    

提交回复
热议问题