在 sql-editor 中,此代码运行良好:
Select *
from Journal
where status<>'D'
order by JDate,J_ID
但无法在 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
如果我只写“order by JDate”或只写“order by J_ID”,那么它就可以工作。
2) 如果我写:
dstJournal.SQL.CommaText:='Select * from Journal where Status<>"D" ';
it gets error:sql error code=-206:Column Unknown D.
我尝试了“D”,但 delphi 无法编译并显示“缺少运算符或分号”消息。
如何编写正确的delphi代码?
最佳答案
不要使用CommaText
。它用于将 SDF 格式(带引号、逗号分隔)的文本解析到 TStrings 对象中或从 TStrings 对象中解析出来。这不是你正在做的事情,所以不要使用它。
设置Text
相反:
dstJournal.SQL.Text := 'SELECT * FROM Journal WHERE status <> ''D'' ORDER BY JDate, J_ID';
或者使用Add()
方法:
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 字符串用单引号括起来,因此如果您打算将它们放入查询中,则必须使用两个单引号对其进行转义。
dstJournal.SQL.Add('WHERE status <> ''D''');
关于sql - 如何在Delphi中正确翻译sql代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53751452/