Search code examples
delphifiredac

Delphi FDQuery.SQL string starting with '!' dont work


I have a query that is dynamically set up with a parameter. I query for lines with a varchar field that contains values that can begin with a '!'. But I get no match of those. I use SQLServer as the database server. If I take the sqlcode and run it directly in the database manager it works but not with TFDQuery. Se the code example below:

  myParameter := '!Tommy';
  with qryExec do
  begin
   SQL.Clear ;
   SQL.Add('SELECT * FROM myTable T WHERE T.Name='+quotedStr(myParameter));
   active := true ;
   first;
    if Not Eof then
    begin
      Result := True;
    end;
  end; //with

I have no idea what's wrong here, so I would be happy if anyone could come with an explanation.


Solution

  • I would suggest using actual parameters which also avoids the possibility of SQL injection. There are also overloaded versions of Open that reduce the housekeeping lines.

      FDQuery1.Open('SELECT * FROM myTable T WHERE T.Name= :NAME',['!Tommy'],[ftWideString]);