Can not catch error in sql [closed]

-1
try
       DModuleGrid.ZQuery1.Close;
       DModuleGrid.ZQuery1.SQL.Clear;
       DModuleGrid.ZQuery1.SQL.Add('SELECT * FROM tdcupant');
       DModuleGrid.ZQuery1.SQL.Add('WHERE numcupom = :co2 AND ccf = :cc3 AND dtcompra = :dtc4 AND impcaixa = :ip5');
       DModuleGrid.ZQuery1.ParamByName('co2').AsString := copy(lTemp,53,6);
       DModuleGrid.ZQuery1.ParamByName('cc3').AsString := copy(lTemp,47,6);
       DModuleGrid.ZQuery1.ParamByName('ip5').AsString := copy(lTemp,4,20);
       DModuleGrid.ZQuery1.ParamByName('dtc4').AsDate := StrToDate(dtcompratxt);
       DModuleGrid.ZQuery1.Open;
     except
       on e: Exception do
       begin
       ercp := e.Message;
       StatusBar1.Panels[0].Text := 'Erro ao encontrar registros! ' + ercp;
     end;

I think it's right. However, it indicates the error of sql itself. Well, the error it gives is:

To be precise, it only needs to catch an exception if none of the records that I compared to the parameters are found in the database.

    
asked by anonymous 16.05.2014 / 15:30

3 answers

0

I debugged the code one more time and found

  DModuleGrid.ZQuery2.Close;
  DModuleGrid.ZQuery2.SQL.Clear;
  DModuleGrid.ZQuery2.SQL.Add('SELECT * FROM tabc460 LIMIT 0, '+valor);
  DModuleGrid.ZQuery2.Open;

This valor , is an increase of case find divergent records. But, think so, if you do not find any record, it will not increase any value right?

Well, it's there, I thought it was only then, why I was browsing the entire database, that's what worked.

if (valor = '') then
  begin
    valor := IntToStr(0);
  end;

I just did this treatment, in case you can not find nada , find 0 ;

    
16.05.2014 / 16:32
1

I do not know your data access component, however, I think it's worth trying:

Replace

       DModuleGrid.ZQuery1.Close;
       DModuleGrid.ZQuery1.SQL.Clear;
       DModuleGrid.ZQuery1.SQL.Add('SELECT * FROM tdcupant');
       DModuleGrid.ZQuery1.SQL.Add('WHERE numcupom = :co2 AND ccf = :cc3 AND dtcompra = :dtc4 AND impcaixa = :ip5');
       DModuleGrid.ZQuery1.ParamByName('co2').AsString := copy(lTemp,53,6);
       DModuleGrid.ZQuery1.ParamByName('cc3').AsString := copy(lTemp,47,6);
       DModuleGrid.ZQuery1.ParamByName('ip5').AsString := copy(lTemp,4,20);
       DModuleGrid.ZQuery1.ParamByName('dtc4').AsDate := StrToDate(dtcompratxt);
       DModuleGrid.ZQuery1.Open;

By:

       DModuleGrid.ZQuery1.Close;
       DModuleGrid.ZQuery1.SQL.Clear;
       DModuleGrid.ZQuery1.SQL.Text := Format('SELECT * FROM tdcupant WHERE numcupom = %s AND ccf = %s AND dtcompra = %s AND impcaixa = %s', [copy(lTemp,53,6),copy(lTemp,47,6),copy(lTemp,4,20),FormatDateTime('yyyymmdd',StrToDate(dtcompratxt))]);
       DModuleGrid.ZQuery1.Open;
    
16.05.2014 / 16:24
1

So, this error is due to a SYNTAX error in the SQL command, debug it and see all the values of the variables that you are using to compose the command, so this takes the contents and runs the direct command in the Database with the values instead of variables.

It will be much easier for you to find the error in the command in this way.

Also check that parameter "IP5" since I did not see it being used anywhere in the command.

I hope it helps.

    
16.05.2014 / 16:55