Thema: Delphi Mit SQL Zeile kopieren ?

Einzelnen Beitrag anzeigen

Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#2

Re: Mit SQL Zeile kopieren ?

  Alt 26. Apr 2007, 15:12
Astraktes Beispiel:
SQL-Code:
INSERT INTO Miep.Mööp(Feld1,Feld2)
  SELECT Feld1
        ,Feld2
  FROM Miep.Mööp
  WHERE PK = 1
Hier das Ganze nochmal generisch. Also Schema, Tabelle, PK-Spalte und den Ursprungs-PK rein und...
SQL-Code:
declare
  type TNameList is table of All_Tab_Columns.Column_Name%type;
  columnNames TNameList;
  sqlStatement clob;

  procedure AddToStatement(aClob in out nocopy clob
                          ,aText in VarChar) is
  begin
    if aText is not null then
      Dbms_Lob.WriteAppend(aClob, Length(aText), aText);
    end if;
  end;

  procedure AddColumnList(aClob in out nocopy clob
                         ,aColumnNames in TNameList) is
  begin
    for i in aColumnNames.first .. aColumnNames.last loop
      if i != aColumnNames.first then
        AddToStatement(aClob, ',');
      end if;
    
      AddToStatement(aClob, '"' || aColumnNames(i) || '"');
    end loop;
  end;
begin

  SELECT t.Column_Name bulk collect
  INTO columnNames
  FROM All_Tab_Columns t
  WHERE t.Owner = :Owner
  and t.Table_Name = :TableName
  and t.Column_Name != :PkColumn
  ORDER BY t.COLUMN_ID;

  if columnNames.Count > 0 then
    Dbms_Lob.CreateTemporary(sqlStatement, true);
  
    AddToStatement(sqlStatement, 'INSERT INTO "' || :Owner || '"."' || :TableName || '"(');
    AddColumnList(sqlStatement, columnNames);
    AddToStatement(sqlStatement, ') SELECT ');
    AddColumnList(sqlStatement, columnNames);
    AddToStatement(sqlStatement, ' FROM "' || :Owner || '"."' || :TableName || '"');
    AddToStatement(sqlStatement, ' WHERE "' || :PkColumn || '" = :Pk');
  
    execute immediate to_Char(sqlStatement)
      Using :PkValue;
  
    Dbms_Lob.FreeTemporary(sqlStatement);
  end if;
end;
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat