I'm developing a program to update some fields in a table in delphi 7 using sql server. the program goes like this:
sql := 'UPDATE tb_dt_contract SET '
+' id_schedule = '+quotedstr(label_id_schedule.Caption)
+',start_date = '+quotedstr(formatdatetime('mm/dd/yyyy',DateTime_start.Date))
+',finish_date = '+quotedstr(formatdatetime('mm/dd/yyyy',DateTime_finish.Date))
+',contract_location = '+quotedstr(uppercase(Edit_location.Text))
+',sign_date = '+quotedstr(formatdatetime('mm/dd/yyyy',DateTime_sign.Date))
+' WHERE id = '+quotedstr(label_id.Caption);
ADOQuery1.Close;
ADOQuery1.SQL.Text := sql;
ADOQuery1.ExecSQL;
when I run the program, it gives me an error: 'Incorrect syntax near '=''. but when I use showmessage to view the query and run it in sql server management 2008, it works fine.
then I try to split the query into parts like this:
SQL := 'UPDATE tb_dt_contract SET '
+' id_schedule = '+QUOTEDSTR(label_id_schedule.CAPTION)
+' WHERE id = '+quotedstr(label_id.Caption);
ADOQuery1.Close;
ADOQuery1.SQL.Text := sql;
ADOQuery1.ExecSQL;
SQL := 'UPDATE tb_dt_contract SET '
+' start_date = '+quotedstr(formatdatetime('mm/dd/yyyy',DateTime_start.Date))
+' WHERE id = '+quotedstr(label_id.Caption);
ADOQuery1.Close;
ADOQuery1.SQL.Text := sql;
ADOQuery1.ExecSQL;
SQL := 'UPDATE tb_dt_contract SET '
+' finish_date = '+quotedstr(formatdatetime('mm/dd/yyyy',DateTime_finish.Date))
+' WHERE id = '+quotedstr(label_id.Caption);
ADOQuery1.Close;
ADOQuery1.SQL.Text := sql;
ADOQuery1.ExecSQL;
SQL := 'UPDATE tb_dt_contract SET '
+' contract_location = '+quotedstr(uppercase(edit_location.Text))
+' WHERE id = '+quotedstr(label_id.Caption);
ADOQuery1.Close;
ADOQuery1.SQL.Text := sql;
ADOQuery1.ExecSQL;
SQL := 'UPDATE tb_dt_contract SET '
+' sign_date = '+quotedstr(formatdatetime('mm/dd/yyyy',DateTime_sign.Date))
+' WHERE id = '+quotedstr(label_id.Caption);
ADOQuery1.Close;
ADOQuery1.SQL.Text := sql;
ADOQuery1.ExecSQL;
then I found out that it only triggers the error when updating a date type field. I have developed other programs to update different tables using similar query and it works fine.. I tried closing the project and reopening it but it still gives me that error message. please tell me what I should do..
To avoid convertions and create database independed save querys you should use parameters, which also could speed up operations if used more than once.
Adoquery1.SQL.Text := 'UPDATE tb_dt_contract SET finish_date=:df where WHERE id =:id';
// in some cases it may be necessary to add the three comented lines
//Adoquery1.Parameters.ParseSQL(Adoquery1.SQL.Text,true);
//Adoquery1.Parameters.ParamByName('df').DataType := ftDateTime;
//Adoquery1.Parameters.ParamByName('id').DataType := ftInteger;
Adoquery1.Parameters.ParamByName('df').Value :=DateTime_finish.Date;
Adoquery1.Parameters.ParamByName('ID').Value :=StrToInt(label_id.Caption);
Adoquery1.ExecSQL;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With