Einzelnen Beitrag anzeigen

Robert_G
(Gast)

n/a Beiträge
 
#5

Re: Zeichen in Feldinhalten per Konsole und SQL ersetzen

  Alt 14. Jul 2004, 12:44
Dein Problem lässt sich IMHO nur mit dynamischem SQL lösen.
Ich habe dir gerade mal was getippt, es ist zwar in PL/SQL, aber hoffentlich einfach genug, damit du es zu IBs PSQL übersetzen kannst.

SQL-Code:
CREATE OR REPLACE PROCEDURE ReplaceSomeChars( pOwner IN Varchar2
                                             ,pSearchStr IN Varchar2
                                             ,pReplStr IN Varchar2) Is
  -- Created on 14.07.2004 12:34:12 by ROBERTG

  -- Local cursors
  Cursor TableCur(i_Owner In Varchar2) Is
    SELECT t.Owner
          ,t.Table_Name
    FROM All_Tables t
    WHERE t.Owner = i_Owner;

  Cursor ColumnCur(i_Owner In Varchar2, i_Table In Varchar2) Is
    SELECT t.Column_Name
    FROM All_Tab_Columns t
    WHERE t.Owner = i_Owner And t.Table_Name = i_Table And
           t.data_type In ('VARCHAR', 'VARCHAR2', 'CHAR');

  -- Local records
  tabRec TableCur%Rowtype;
  ColRec ColumnCur%Rowtype;

  -- Local variables
  SQLStmt Varchar2(6000);
Begin

  For tabRec In TableCur(upper(Trim(pOwner))) Loop
    SQLStmt := Null;
    Open ColumnCur(tabRec.Owner
                  ,tabRec.Table_Name);
  
    Fetch ColumnCur
      INTO ColRec;
    While Not ColumnCur%NotFound Loop
      SQLStmt := SQLStmt || chr(10) || --
                 ' ,' || ColRec.Column_Name || ' = replace(' ||
                 ColRec.Column_Name || ', lSearchStr, lReplStr)';
    
      Fetch ColumnCur
        INTO ColRec;
    End Loop;
    If ColumnCur%Rowcount > 0 Then
      Execute Immediate 'Declare' || chr(10) || --
                        ' lSearchStr Char('||length(pSearchStr)||') := :iSearchStr;' || chr(10) || --
                        ' lReplStr Char('||length(pReplStr)||') := :iReplStr;' || chr(10) || --
                        'Begin' || chr(10) || --
                        ' Update ' || tabRec.Owner || '.' || tabRec.Table_Name || chr(10) || --
                        ' SET ' || SubSTR(SQLStmt, 12) || ';' || chr(10) || --
                        'End;
        Using pSearchStr, pReplStr;
    End If;
    Close ColumnCur;
  End Loop;
End;
Edit: Tippfehler...
  Mit Zitat antworten Zitat