Hi... I'm having some problems to get the last_insert_id after a insert query... I'm getting always 0, the problem (I suppose) is that I have to do everything in the same transaction...

I paste you some code here to help me to understand the problem (id_ingresso is the variable I get always = 0):

procedure TMyClass.InsertIntotheDB(Connection: TSQLConnection); // i pass the connection from another class (to be sure it's open and alive)
var
 Qry, QryRead: TSQLQuery;
 Trans: TSQLTransaction;
begin

 //parameters
 Trans:=TSQLTransaction.Create(nil);
 Qry:=TSQLQuery.Create(nil);
 QryRead:=TSQLQuery.Create(nil);
 try
  Connessione.Transaction := Trans;
  Qry.DataBase := Connessione;
  Qry.Transaction := Trans;
  QryRead.DataBase := Connessione;
  QryRead.Transaction := Trans;

  Qry.ParseSQL := true;
  Qry.ReadOnly := false;
  QryRead.ParseSQL := false;
  QryRead.ReadOnly := true;


  Qry.Active := false;
  Qry.SQL.Text := 'INSERT INTO ECCETERA';
  Qry.Params.ParamByName('PK_parameter').AsInteger := self.id_parameter;
  try
     Qry.ExecSQL;
  except
     Log.Scrivi(0,'Error'+QryRead.SQL.Text+'"');
  end;

  QryRead.Active := false;
  QryRead.SQL.Text := 'SELECT LAST_INSERT_ID() as PK_INGRESSO';
  try
    QryRead.open;
    self.id_ingresso := QryRead.FieldByName('PK_parameter').AsInteger;
    QryRead.close;
  except
     Log.Scrivi(0,'Error'+QryRead.SQL.Text+'"');
     self.id_ingresso := -1;
  end;

Log.Scrivi(6, 'Logica> Passaggio registrato sul DB. Pk_ingresso = '+IntToStr(self.id_ingresso));

 finally
  Qry.Free;
  QryRead.Free;
  Trans.Free;
 end;

end;

end.

_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to