title: How to bulk copy the values from a TSQLQuery record to a TdxMemData record?
body: <p>Just to make it clear, I do not want to copy the entire TSQLQuery into the TdxMemData, as I would use memds.CopyFromDataSet(qry) for that.</p>
  <p>I am interating through each record from the TSQLQuery, and I may or may not be adding a record(s) to the TdxMemData. Generally the record in memds matches that in qry, but sometimes the values are altered and sometimes additional records are added to memds. My example did not make this clear since all it seemed to do was copy over each record.</p>
  <p>So given an active record in the TSQLQuery, I want to copy over the values into an active editable record in the TdxMemData.</p>
  <p>The following code works in so far as it creates a copy of the record:</p>
  <pre><code>qry := TSQLQuery.Create(nil);
  memds := TdxMemData.Create(nil);
  
  try
    qry.SQLConnection := cn;
    qry.Text := 'SELECT Field1, Field2, Field3 FROM Table1';
    qry.Open
    
    memds.CreateFieldsFromDataSet(qry);
    memds.Open;
  
    while not qry.Eof do
    begin
      if {some condition} then
      begin
        memds.Append;
        
        for i := 0 to qry.FieldCount-1 do
          memds.Fields[i+1].Value := qry.Fields[i].Value; //First field is RecID
      
        //Do something with the current memds record
      end
      else if {some other condition} then
      begin
        memds.Append;
        //change values
        memds.Append;
        //change values
        memds.Append;
        //change values
      end
      else if {a third condition} then
        ; //Skip any work on memds
      qry.next;
    end;
    
    qry.Close;
    
    //Do something with memds
    
    memds.Close;
  finally
    memds.Free;
    qry.Free;
  end;
  </code></pre>
  <p>Is there a better way? I had looked at AppendRecord but creating the array of TVarRec doesn't seem to be straightforward.</p>
  <p>EDIT:</p>
  <p>Let's use these examples with very simplified criteria. Note that the actual conditions that determine how many records to append and the changes to the field values in the destination are complex and not in any database.</p>
  <p>Method 1:</p>
  <pre><code>While not tblSource.Eof do
  Begin
    If (iCondition = 1) Then
    Begin
      // Add one record
  
      tblDestination.Append;
  
      tblDestination.FieldByName('Field1').Value := tblSource.FieldByName('Field1').Value; 
      tblDestination.FieldByName('Field2').Value := tblSource.FieldByName('Field2').Value; 
      tblDestination.FieldByName('Field3').Value := tblSource.FieldByName('Field3').Value; 
      tblDestination.FieldByName('Field4').Value := tblSource.FieldByName('Field4').Value; 
      tblDestination.FieldByName('Field5').Value := tblSource.FieldByName('Field5').Value; 
  
      if bSomethingCondition then
        tblDestination.FieldByName('Field4').Value := 'Something';
    End
    Else If (iCondition = 2) Then
    Begin
      // Add two records
  
      tblDestination.Append;
  
      tblDestination.FieldByName('Field1').Value := tblSource.FieldByName('Field1').Value; 
      tblDestination.FieldByName('Field2').Value := tblSource.FieldByName('Field2').Value; 
      tblDestination.FieldByName('Field3').Value := tblSource.FieldByName('Field3').Value; 
      tblDestination.FieldByName('Field4').Value := tblSource.FieldByName('Field4').Value; 
      tblDestination.FieldByName('Field5').Value := tblSource.FieldByName('Field5').Value; 
  
      if bAnotherThingCondition then
        tblDestination.FieldByName('Field4').Value := 'Another thing'; 
  
      tblDestination.Append;
  
      tblDestination.FieldByName('Field1').Value := tblSource.FieldByName('Field1').Value; 
      tblDestination.FieldByName('Field2').Value := tblSource.FieldByName('Field2').Value; 
      tblDestination.FieldByName('Field3').Value := tblSource.FieldByName('Field3').Value; 
      tblDestination.FieldByName('Field4').Value := tblSource.FieldByName('Field4').Value; 
      tblDestination.FieldByName('Field5').Value := tblSource.FieldByName('Field5').Value; 
  
      if bSomethingElseCondition then
        tblDestination.FieldByName('Field4').Value := 'Something else'; 
    End
    Else If (iCondition = 0) Then
    Begin
      // Add no records
    End;
  
    tblSource.Next;
  End;
  </code></pre>
  <p>Since the number of fields in the source and destination tables can vary, hard-coding field names as in Method 1 is not suitable.</p>
  <p>Method 2:</p>
  <pre><code>While not tblSource.Eof do
  Begin
    If (iCondition = 1) Then
    Begin
      // Add one record
  
      tblDestination.Append;
  
      for i := 0 to tblSource.FieldCount-1 do
        tblDestination.Fields[i+1].Value := tblSource.Fields[i].Value;
  
      if bSomethingCondition then
        tblDestination.Fields(iSomethingConditionFieldIndex).Value := 'Something';
    End
    Else If (iCondition = 2) Then
    Begin
      // Add two records
  
      tblDestination.Append;
  
      for i := 0 to tblSource.FieldCount-1 do
        tblDestination.Fields[i+1].Value := tblSource.Fields[i].Value;
  
      if bAnotherThingCondition then
        tblDestination.Fields(iAnotherThingConditionFieldINdex).Value := 'Another thing'; 
  
      tblDestination.Append;
  
      for i := 0 to tblSource.FieldCount-1 do
        tblDestination.Fields[i+1].Value := tblSource.Fields[i].Value;
  
      if bSomethingElseCondition then
        tblDestination.Fields(iSomethingElseConditionFieldIndex).Value := 'Something else'; 
    End
    Else If (iCondition = 0) Then
    Begin
      // Add no records
    End;
  
    tblSource.Next;
  End;
  </code></pre>
  <p>While method 2 above does work, and is the way it is currently done, this question is whether there is a way to pass the variant array of field values from tblSource to tblDestination using AppendRecord.</p>
  <p>Instead of this:</p>
  <pre><code>// Add one record
  
  tblDestination.Append;
  
  for i := 0 to tblSource.FieldCount-1 do
     tblDestination.Fields[i+1].Value := tblSource.Fields[i].Value;
  
  if bSomethingCondition then
    tblDestination.Fields(iSomethingConditionFieldIndex).Value := 'Something';
  </code></pre>
  <p>Do this:</p>
  <pre><code>tblDestination.AppendRecord({tblSource fields var array);
  
  if bSomethingCondition then
    tblDestination.Fields(iSomethingConditionFieldIndex).Value := 'Something';
  </code></pre>
  <p>Of course, it might be that there is no answer, and that the method I currently employ is the best solution.</p>