1

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.

4
  • 2
    Have you seen stackoverflow.com/questions/59812774/…? Commented Nov 26, 2021 at 19:10
  • 2
    You might have a look at docwiki.embarcadero.com/RADStudio/Sydney/en/… Commented Nov 26, 2021 at 19:28
  • 1
    May be a parameters pb, try ResourceOptions.ParamCreate := False Commented Nov 26, 2021 at 20:20
  • Thanks a lot, really fast answers :). I found that Andreas and MartynA answers is about the same. I haven't tried philnext:s solution, it would probably work but I love Brians solution below. It's really simple and nice. Commented Nov 27, 2021 at 8:40

1 Answer 1

1

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]);
Sign up to request clarification or add additional context in comments.

Comments

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.