在delphi查询中使用@variables:=不起作用
我有以下问题。
ZQuery1.SQL.Text:=
' SELECT '+
' IF(q.rank2 = 1, @rank:= 1, @rank:= @rank + 1) AS rank '+
' ,q.* FROM ( '+
' SELECT groep.id - MinGroepId(groep.id) AS rank2 '+
' ,groep.otherfields '+
' FROM groep '+
' ORDER BY rank2 ) q; ';
ZQuery.Open;
当我运行此代码时,我在 ZQuery1 中收到异常不正确的标记后跟“:”
。
我该如何解决这个问题?我需要使用 Delphi,因为我无法将此选择放入 MySQL 过程中。
Zeos 6 不支持返回结果集的 MySQL 过程。
附注
我正在使用 Delphi 2007 和 MySQL 5.1 以及 ZEOS 6.6.6。
尽管我很确定版本并不重要。
我不愿意切换版本,因为我对项目的了解太深了。
I have the following problem.
ZQuery1.SQL.Text:=
' SELECT '+
' IF(q.rank2 = 1, @rank:= 1, @rank:= @rank + 1) AS rank '+
' ,q.* FROM ( '+
' SELECT groep.id - MinGroepId(groep.id) AS rank2 '+
' ,groep.otherfields '+
' FROM groep '+
' ORDER BY rank2 ) q; ';
ZQuery.Open;
When I run this code I get an exception Incorrect token followed by ":"
in ZQuery1.
How do I fix this? I need to use Delphi, because I cannot put this select in a MySQL procedure.
Zeos 6 does not support MySQL procedures that return a resultset.
P.S.
I'm using Delphi 2007 and MySQL 5.1 with ZEOS 6.6.6.
Although I'm pretty sure the versions don't matter.
I'm not willing to switch versions as I'm too far into the project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MySQL 能够拥有引用 @ 的用户变量(基于会话)(所以我不想说 LaKraven 有点偏离主题)。
我在工作中使用 Dac for MySQL (http://www.microolap.com/products/connectivity/mysqldac/) 时遇到了同样的问题。他们通过进行特殊检查以查看“:”后面的字符是否为“=”来进行纠正,如果是,则不会发生参数替换。
我对 Zeos 组件了解不多,所以我唯一可以建议的是跟踪执行路径并查看异常发生的位置,并修补代码以处理 ':=' 的字符序列
MySQL has the ability to have user variables (session based) that are referred to @ (so I hate to say that LaKraven is slightly off the mark).
I had the same problem with the Dac for MySQL (http://www.microolap.com/products/connectivity/mysqldac/) at work. They corrected by putting in special check to see if the character after ':' was an '=' and if it was, parameter replacement did not occur.
I do not know all that much about the Zeos components, so the only thing I can suggest is that you trace down the path of execution and see where the exception is occurring and patch the code to handle the character sequence of ':='
我不知道这里是否是这种情况,但是您的 SQL 中有错误:
IF
中的分号应替换为逗号,ASrank
后缺少逗号group
是保留字,因此当用作表名时,应在 `` 中引用它。I don't know if this is the case here, but you have errors in your SQL: semicolon in
IF
should be replaced with comma, there is comma missing afterAS rank
andgroup
is reserved word so when used as table name it should be quoted in `` .尝试将 TZQuery.ParamCheck 设置为 False。当“:”是参数标记时,这将禁用自动参数创建。
Try to set TZQuery.ParamCheck to False. That will disable automatic parameters creation, when ':' is a parameter marker.
好的,我破解了一个解决方案。
但它确实很难看,但它仍然有效(有点)。
编辑,这个在 dbForge-MySQL 和 Delphi 中工作
首先,我在 MySQL 中创建了一个存储函数“ranking”,它在 @rank 中存储一个值和/或偏移量。
接下来,我将 ZQuery1 更改为如下内容:
这有效,Delphi 中的完整复杂代码也有效。
(-_-')
再次战胜邪恶机器
所以回顾一下。
@varname 在存储过程内(当然在单个连接内)是持久的。
在 select 语句和存储过程之间交换 @varname 在 dbForge 中有效,但在 Delphi 中失败。
OK, I hacked a solution.
But it sure is ugly, still it works (sorta).
EDIT, this one works in dbForge-MySQL and Delphi
First I created a stored function 'ranking' in MySQL, that stores a value and/or offset in @rank.
Next up, I changed the ZQuery1 to read something like:
This works, and the full complex code in Delphi also works.
(-_-')
Another triumph over the evil machines
So to recap.
@varname is persistent within a stored procedure (inside a single connection of course).
Exchanging @varname between the select statement and the stored procedure works in dbForge, but fails in Delphi.
这是不行的,只能参数化值。
您可以做的最好的事情是 SQL.Text := StringReplace() 但您会失去准备查询的速度。
This can't be done, you can only parameterize the value.
Best you can do is SQL.Text := StringReplace() but you lose the speed of preparing queries.