1

我想遍历所有列以查找和替换特定字符。这是我的 pl/sql 块:

 Declare
  match_count     Number:=0;
  v_search_string VARCHAR2(4000) := '%ي%';
BEGIN
 FOR t IN
 (SELECT owner,
  table_name,
column_name
FROM all_tab_columns
 WHERE (SUBSTR(table_name,1,2)='PN'
OR (SUBSTR(table_name,1,2)   ='CD'
AND owner                    ='PNET_USER' ))
AND (data_type               ='VARCHAR2'
OR data_type                 ='CLOB')
)
LOOP
BEGIN
  EXECUTE IMMEDIATE 'SELECT count(*) FROM '||t.owner || '.' || t.table_name|| ' WHERE '||t.column_name||' LIKE :1' INTO match_count USING v_search_string;
  IF match_count > 0 THEN
    dbms_output.put_line( t.owner || '.' || t.table_name ||' '||t.column_name||' '||match_count );
    --EXECUTE IMMEDIATE 'UPDATE '||t.table_name||' SET '||t.column_name||'=replace()'
  END IF;
END;
END LOOP;

它工作正常并打印具有无效字符的列的名称。但我不知道如何替换字符。我怎样才能得到的值t.column_name,替换无效字符然后更新t.table_name

4

2 回答 2

2

好吧,这几乎没问题(还有stevo的回答)。我让你用你的字符改变 v_from 和 v_to 。

Declare
  match_count     Number       :=0;
  v_from          varchar2(5)  := 'a';
  v_like          varchar2(5)  := '%'||v_from||'%';
  v_to            varchar2(5)  := 'b';
  v_sql           varchar2(1000);
  v_emesg         varchar2(1000);

  CURSOR s is
        (SELECT owner, table_name, column_name
        FROM    all_tab_columns
        where   SUBSTR(table_name,1,2)    IN ('PN', 'CD')
        AND     owner                     ='PNET_USER' 
        AND     data_type                 IN('VARCHAR2', 'CLOB'););
begin       
for t in s  LOOP
     begin
      EXECUTE IMMEDIATE 'SELECT count(*) FROM '||t.owner || '.' || t.table_name|| ' WHERE '||t.column_name||' LIKE :1' INTO match_count USING v_like;
      IF match_count > 0 THEN
            begin
            dbms_output.put_line( t.owner || '.' || t.table_name ||' '||t.column_name||' '||match_count );
            v_sql := 'UPDATE '||t.owner||'.'||t.table_name||' SET '||t.column_name||'= REPLACE('||t.column_name||', '''||v_from||''', '''||v_to||''') WHERE '||t.column_name||' LIKE '''|| v_like||'''';
            dbms_output.put_line(v_sql);

            EXECUTE IMMEDIATE v_sql;
            EXCEPTION WHEN OTHERS THEN
               v_emesg := SQLERRM;
               dbms_output.put_line(v_emesg);
               dbms_output.put_line('Errow while trying to update '||t.owner||'.'||.t.table_name||' : column '||t.column_name||'.';
            END;
      END IF;
      end;
  END LOOP;
end;
于 2012-05-13T22:41:25.577 回答
-1

就像是

立即执行'更新'||t.table_name||' SET '||t.column_name||'=replace('||t.column_name||',''FROM'',''TO'')'

应该这样做,其中 FROM 是无效字符,而 TO 是您想要将其更改为的任何字符。有关 REPLACE 工作原理的说明,请参阅链接中的 oracle 文档。

http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions134.htm

注意我没有任何地方可以尝试这个,所以语法可能不正确。

于 2012-05-13T16:11:21.123 回答