Get SQL before POST in a Zquery

Asked

Viewed 722 times

0

Guys, does anyone know how to get sql executed in the POST of a query in Insert mode? For example, I have the student table with id and name:

begin
    tblAluno.Insert;
    tblAlunoID.asInteger := 1;
    tblAlunoNome.asString := 'Carlos';
    //aqui
    tblAluno.Post;
end;

Just in "here" I want to get the sql that runs underneath with Insert. Note that if I put: Clipboard.astext := tblAluno.sql.Text it returns me the query sql and not what actually ran in the post.

2 answers

1

You can get sql from Updateobject ex.:

ZQuery.UpdateObject.InsertSQL;
ZQuery.UpdateObject.DeleteSQL;
ZQuery.UpdateObject.ModifySQL;

@Jack, then you can try it this way:

var
   strLista: TStrings;
   idx: Integer;
begin
   strLista := nil;
   try
      strLista := TStringList.Create;
      strLista.Text := ZQuery.UpdateObject.InsertSQL.Text;
      strLista.Add('=======================================')
      for idx := 0 to ZQuery.Fields.Count - 1 do
      begin
         strLista.Add(ZQuery.Fields[idx].Name + ' = ' + ZQuery.Fields[idx].AsString);
      end;

      Clipboard.AsText := strLista.Text;
   finally
      if Assigned(strLista) then
         FreeAndNil(strLista);
   end;
end;

it will take sql, and at the end it will add the parameters and their respective values, (should be placed in the before post of zquery)

  • Brother @dennis90, had tried this: Showmessage(Zquery1.UpdateObject.Insertsql.Text); Showmessage(Zquery1.UpdateObject.SQL[ukInsert]. Text); 1.UpdateObject. SQL[ukInsert]. Text); E memory error occurs when accessing the property.

  • there is some component defined in the Updateobject property of zquery?

  • In the Updateobject property there is no component. But in the Updatemode property it is like: umUpdateChanged

  • If I put a Tzupdatesql object in the query works, it accesses the Updateobject.Insertsql property, but it shows it with the unfilled parameters: update table set field1 = :value. I need to access sql without the update component and before the post with the fields filled.

  • I edited the message, check the new code added.

  • beauty, it is still a solution, but imagine if you have a query with more than 20 fields, I will need to take the Clipboard and replace parameter by parameter in sql for ai yes run in ibexpert until I find the error. Not to mention that this code only works if the query is linked to a Zupdatesql component, otherwise I will gain an Exception.

  • Friend, sorry for the delay in answering, but if you have any error in the query and you do not have componentne Updatesql assigned, this error will not be by query SQL, try to rephrase the question with the error you are facing that we will try to help.

  • Well @dennis90, I have no error, I only need the sql of Insert that runs when I do the post as I mentioned in the question up there. And I don’t have an updateSQL component connected to the query.

  • That’s why the Querytable standard of Delphi is very good, because it mounts the 4 queries for you. Pity that does not have that in Zeos.

Show 4 more comments

0

Jack, I recommend you don’t use ZTable, only in very extreme cases. Play a ZQuery in the DataModule and assign to your connection. Then play a ZUpdateSQL and attribute it to ZQuery on the property UpdateSQL. In his ZQuery modify the SQL.Text property to 'SELECT * FROM Tabela WHERE algumacoisa = algumacoisa;'. Then double click on ZUpdateSQL and a window will open. Click 'Generate SQL' and he will make exquisitely the 3 codes, to DELETE, UPDATE and INSERT. You can then edit them and improve them any way you want.

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • Place WHERE id = :id; at the end of UPDATE and DELETE if you use this type of field to differentiate each record and not end up going through other records.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.