0

Why is it that my query does not work ?

Form1.ABSQuery1.Close;
Form1.ABSQuery1.SQL.Clear;
Form1.ABSQuery1.SQL.Text:='DELETE FROM LOG WHERE status = ''YES'' and DATE BETWEEN :d1 and :d2';
Form1.ABSQuery1.Params.ParamByName('d1').Value :=cxDateEdit1.Date;
Form1.ABSQuery1.Params.ParamByName('d2').Value :=cxDateEdit2.Date;
Form1.ABSQuery1.ExecSQL;
Form1.ABSTable1.Refresh;

I get this error :

enter image description here

4
  • 1
    What doesn't work? Does it not delete the record(s) you expect? Do you get an SQL Error? Can you provide more info on what the actual issue is
    – Jason
    Commented Jun 18, 2013 at 22:44
  • What happens if you parenthesize: (DATE BETWEEN :d1 and :d2)?
    – lurker
    Commented Jun 18, 2013 at 22:59
  • It could be DATE is being interpreted as a function maybe? Is DATE really the name of one of your columns? If so, rename your DATE field to something else and try again.
    – Jason
    Commented Jun 18, 2013 at 23:03
  • 'delete from LOG where status = ''YES'' and (DATE BETWEEN :d1 and :d2)'; - nothing gets deleted ...
    – user763539
    Commented Jun 18, 2013 at 23:09

2 Answers 2

1

You should be using AsDateTime in your Params setting code.

Form1.ABSQuery1.SQL.Text:='DELETE FROM LOG WHERE status = ''YES'' and DATE BETWEEN :d1 and :d2';
Form1.ABSQuery1.Params.ParamByName('d1').AsDateTime :=cxDateEdit1.Date;
Form1.ABSQuery1.Params.ParamByName('d2').AsDateTime :=cxDateEdit2.Date;
Form1.ABSQuery1.ExecSQL;

Using Value converts the cxDateEdit1.Date to a generic string format for assignment, and that doesn't properly convert it to the YYYY-MM-DD format that most databases (including ABS) expect. Properly using AsDateTime allows the database driver/component to convert to the specific date format the DBMS uses.

Also, is your database field really named DATE? Date is usually a reserved word or function name in most DBMS, and if it is it usually needs to be quoted.

1
  • It had me baffled for a while as I, at first, used .asDate. Adding time, it seems , does the trick.
    – user763539
    Commented Jun 18, 2013 at 23:21
0
Form1.ABSQuery1.Params.ParamByName('d1').DataType := ftDateTime;
Form1.ABSQuery1.Params.ParamByName('d1').Value :=cxDateEdit1.Date;

You must explicitly specify the data type of the parameter to it had no such problem, and then convert to a string does not need to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.