Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi Mit SQL Zeile kopieren ? (https://www.delphipraxis.net/90957-mit-sql-zeile-kopieren.html)

HolgerCW 26. Apr 2007 14:47

Datenbank: ORACLE • Version: 9 • Zugriff über: BDE

Mit SQL Zeile kopieren ?
 
Hallo zusammen,

wie kann ich am einfachsten eine Zeile mit SQL kopieren?

Möchte in einer Tabelle die Zeile mit der ID 2, davon die Spalten 5 - 30 auf die Zeile mit der ID 5 kopieren.

Die Spalte ID heisst auch ID, nur die anderen Spalten haben ganz unterschiedliche Bezeichnungen.
Möchte halt nicht die ganze Zeile kopieren, sondern nur die Spalten 5 - 30 (In denen nur 1 oder NULL steht) der jeweiligen Spalte.

Hat das wer eine Lösung ?

Gruss

Holger

Elvis 26. Apr 2007 15:12

Re: Mit SQL Zeile kopieren ?
 
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:46 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz