How to copy Data inside one table

Discussion of open issues, suggestions and bugs regarding ODAC (Oracle Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
FanderlF
Posts: 17
Joined: Thu 21 Dec 2006 13:43

How to copy Data inside one table

Post by FanderlF » Tue 24 Apr 2007 07:54

Hey,
I need to build a new row (containing numbers, blobs...) copying an old row and changing some values.
Is there any possibility to do this without using an SQL Query? I tried to use 2 TOraTables on the same table, but then I got a debugger exception (seems not to be considered to be used that way).
Is there an easy way to coyp Data from one row to another?

Thanks in advance,
Florian Fanderl

jfudickar
Posts: 202
Joined: Fri 10 Mar 2006 13:03
Location: Oberursel / Germany

Post by jfudickar » Tue 24 Apr 2007 10:19

Without sitting on my development-system:

Code: Select all

dataset.insert;
dataset.fieldbyname('Field1').AsString := 'A';
dataset.fieldbyname('Field12').AsInteger := 3;
dataset.post;
Greetings
Jens

FanderlF
Posts: 17
Joined: Thu 21 Dec 2006 13:43

Post by FanderlF » Tue 24 Apr 2007 11:10

Hmmm... I was just wondering if there wasn't a general functionality. But that helps :) at least I can copy it by hand, if the table doesnt get too big. Though I'd prefer a more general row copy function, cause dealing with Blobs (Streams) is a bit annoying.

Plash
Devart Team
Posts: 2844
Joined: Wed 10 May 2006 07:09

Post by Plash » Tue 24 Apr 2007 12:25

There is no procedure that copies data from one row to another in ODAC. You should manually copy each value.

FanderlF
Posts: 17
Joined: Thu 21 Dec 2006 13:43

Post by FanderlF » Wed 25 Apr 2007 06:59

ok :) thank you... then I'll do it that way.

robert.wachtel
Posts: 13
Joined: Wed 12 Oct 2005 13:41

Post by robert.wachtel » Wed 25 Apr 2007 20:22

Code: Select all

procedure DuplicateCurrentRecord(aDataSet: TDataSet);
var
  Data: array of variant;
  aRecord: array of TVarRec;
  i: integer;
  max: integer;
begin
  max := aDataSet.fields.count - 1;
  // set the lenghth of the arecord array to be the same as the number of
  // elements in the data array
  SetLength(arecord, max + 1);
  SetLength(data, max + 1);

  // set the variant type pointers to the data array
  for i := 0 to max do
  begin
    arecord[i].VType := vtVariant;
    arecord[i].VVariant := @data[i];
  end;

  // Copy the Record to the Array
  for i := 0 to max do
    Data[i] := aDataSet.fields[i].value;

  aDataSet.Insert;
  aDataSet.SetFields(aRecord);
end;

FanderlF
Posts: 17
Joined: Thu 21 Dec 2006 13:43

Post by FanderlF » Thu 26 Apr 2007 07:00

Great :) thank you... I'll test it immediately :D 8)

Post Reply