Reputation: 431
In sql-editor this code works fine:
Select *
from Journal
where status<>'D'
order by JDate,J_ID
but couldn't write correct code in Delphi.
1)
dstJournal.Close;
dstJournal.SQL.Clear;
dstJournal.SQL.CommaText:='Select * from Journal order by JDate,J_ID';
sql error code=-104: Token Unknown J_ID
if I write only 'order by JDate' or only 'oder by J_ID' then it works.
2) if I write:
dstJournal.SQL.CommaText:='Select * from Journal where Status<>"D" ';
it gets error:sql error code=-206:Column Unknown D.
I tried 'D' but delphi couldn't compile and shows "missing operator or semicolon" message.
How to write correct delphi code?
Upvotes: 1
Views: 256
Reputation: 31463
Don't use CommaText
. It is for parsing SDF formatted (quoted, comma delimited) text into or out of a TStrings
object. That's not what you're doing, so don't use that.
Set the Text
instead:
dstJournal.SQL.Text := 'SELECT * FROM Journal WHERE status <> ''D'' ORDER BY JDate, J_ID';
Or use the Add()
method:
dstJournal.SQL.Clear;
dstJournal.SQL.Add('SELECT * FROM Journal');
dstJournal.SQL.Add('WHERE status <> ''D''');
dstJournal.SQL.Add('ORDER BY JDate, J_ID');
I tried 'D' but delphi couldn't compile: missing operator or semicolon.
Delphi strings are enclosed with single quotes so if you mean to put them into your query you have to escape them by using two single quotes.
dstJournal.SQL.Add('WHERE status <> ''D''');
Upvotes: 3