Ok - after playing with Format was able to execute with results.. here is the working code.Code: [Select]Query.SQL.Clear;
Query.SQL.Text := 'SELECT * FROM EMPTBL ' + Format('WHERE CITY LIKE "%%%s%%"', [cityname]);
Query.Prepare;
Query.Open;
Sorry for the confusion.
Actually the SQL Statement was right (was trying to give an example using Address/City".. just the formatting was not working..
Original query:
Query.Params.ParamByName('CITY').AsString := '%Dallas%'; - results in exceptions..
Modified query: using format string
Format('WHERE CITY LIKE "%%%s%%"', [cityname]); // Worked.
Do you know how to use the above query with Params??
Query.SQL.Clear;
// note the WHERE clause
Query.SQL.Text := 'SELECT * FROM EMPTBL WHERE CITY LIKE :PVal';
Query.ParamCheck := True;
Query.Prepare;
Query.Params.ParamByName('PVal').AsString := '%Dallas%';
Query.Open;
Original query:Also always provide us with WHAT exceptions you get.
Query.Params.ParamByName('CITY').AsString := '%Dallas%'; - results in exceptions..
MySQLQuery.SQL.Text:='SELECT * FROM Manufacturer WHERE Name LIKE :TEMP';
MySQLQuery.ParamByName('TEMP').AsString:='%mark%';
MySQLQuery.Open;
Writeln(MySQLQuery.FieldByName('Name').AsString); //--> Returns 'Lexmark'
The only difference here:You shouldn't quote parameters. So the quotedstr() and Format() variants will not work.
I assign variable name - docName to
Query.Params.ParamByName('Pval').AsString := '%'+docName+'%';
Or
Query.Params.ParamByName('Pval').AsString := quotedstr('%'+docName+'%');
Or
Query.Params.ParamByName('Pval').AsString := Format('"%%%s%%"', [docName]);
None works - no errors or exceptions.. except no results selected.
Yes - I too was referring to this statement:
Query.Params.ParamByName('Pval').AsString := '%'+docName+'%';
However, I'm still using the format() - which is working - [...]