简体   繁体   English

查询适用于sql server management studio 2008但不适用于delphi

[英]query works on sql server management studio 2008 but not in delphi

I'm developing a program to update some fields in a table in delphi 7 using sql server. 我正在开发一个程序来使用sql server更新delphi 7中表中的某些字段。 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. 但是当我使用showmessage查看查询并在sql server management 2008中运行它时,它工作正常。

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;

I figured it out! 我想到了! (more like, my colleague did) (更像是,我的同事呢)

apparently after replacing the FormatDateTime('mm/dd/yyyy' with datetostr and setting the shortdateformat to 'mm/dd/yyyy', the query works fine. I wonder why that is, though? I thought those commands do the same thing? 显然在用Datetostr替换FormatDateTime('mm / dd / yyyy'并将shortdateformat设置为'mm / dd / yyyy'后,查询工作正常。我想知道为什么会这样呢?我认为那些命令做同样的事情?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM