FireDAC Query dropping special characters in SQL statement to SQL Server

你离开我真会死。 提交于 2021-01-28 08:13:24

问题


I have hit this issue off and on in Delphi 10.3 using FireDAC and the EMS Rad Server. I have not experienced it in Delphi 10.2 or below, but I am not using FireDAC anywhere but in Delphi 10.3. The issue I am experiencing is some special characters seem to be getting stripped out of the SQL statements before they reach the Database.

For example, if I run:

update messageread set
MessageDeliveredDateTime = '8/11/2020 6:33:45 PM'
where messageread.dts in ('5/7/2020 12:48:20 PM-!+[[786',   '5/7/2020 12:47:06 PM-!#[[782', '5/7/2020 12:43:35 PM-&K[[775', '5/7/2020 12:41:01 PM-&K[[773')

what gets executed on the SQL server is:

update messageread set MessageDeliveredDateTime = '8/11/2020 6:33:45 PM' where messageread.dts in ('5/7/2020 12:48:20 PM-+[[786',   '5/7/2020 12:47:06 PM-[[782',   '5/7/2020 12:43:35 PM-[[775',   '5/7/2020 12:41:01 PM-[[773')

It appears to be dropping 2 characters after the "-". Because it is 2 characters, it makes me thing its some Unicode thing. In the past, I have worked around this by using parameterized queries, but in this case, it still isn't helping. The Delphi code that I am currently running is:

fdTemp.SQL.Text := 'update messageread set MessageDeliveredDateTime = ' + QuotedStr(DateTimeToStr(now)) + ' where messageread.dts in (' + sUpdateDTS + ')';
fdTemp.ExecSQL;
sUpdateDTS = '5/7/2020 12:48:20 PM-!+[[786',    '5/7/2020 12:47:06 PM-!#[[782', '5/7/2020 12:43:35 PM-&K[[775', '5/7/2020 12:41:01 PM-&K[[773'

Where fdTemp is TFDQuery and DTS is the primary key of the table. If I take the SQL statement and run it in Mgt Studio, it works just fine. However when run from Delphi, 0 rows are affected because nothing matches the where clause.

Does anyone have any idea?


回答1:


Some characters in SQL commands have special meaning in FireDAC and thus have to be entered in a special way. In your case an identifier starting with & is treated as a macro.

You can suppress that by setting ResourceOptions.MacroCreate to false.

More info on special character handling in FireDAC can be found in the documentation: Special Character Processing



来源:https://stackoverflow.com/questions/63368496/firedac-query-dropping-special-characters-in-sql-statement-to-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!