在delphi查询中使用@variables:=不起作用

发布于 2024-10-31 22:41:23 字数 761 浏览 4 评论 0原文

我有以下问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

失眠症患者 2024-11-07 22:41:24

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 ':='

じее 2024-11-07 22:41:24

我不知道这里是否是这种情况,但是您的 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 after AS rank and group is reserved word so when used as table name it should be quoted in `` .

月亮邮递员 2024-11-07 22:41:24

尝试将 TZQuery.ParamCheck 设置为 False。当“:”是参数标记时,这将禁用自动参数创建。

Try to set TZQuery.ParamCheck to False. That will disable automatic parameters creation, when ':' is a parameter marker.

深居我梦 2024-11-07 22:41:24

好的,我破解了一个解决方案。
但它确实很难看,但它仍然有效(有点)。

编辑,这个在 dbForge-MySQL 和 Delphi 中工作

首先,我在 MySQL 中创建了一个存储函数“ranking”,它在 @rank 中存储一个值和/或偏移量。

CREATE DEFINER = 'root'@'localhost'
FUNCTION MyDatabase.Ranking(NewRank INT, Addition INT)
  RETURNS int(11)
BEGIN
  IF NOT(NewRank IS NULL) THEN SET @rank:= NewRank; END IF;
  IF NOT(Addition IS NULL) THEN SET @rank:= @rank + Addition; END IF;
  RETURN @rank;   
END

接下来,我将 ZQuery1 更改为如下内容:

select ranking(null,1) as rank
  ,groep.*
  from groep
join (select ranking(0,null)) r

这有效,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.

CREATE DEFINER = 'root'@'localhost'
FUNCTION MyDatabase.Ranking(NewRank INT, Addition INT)
  RETURNS int(11)
BEGIN
  IF NOT(NewRank IS NULL) THEN SET @rank:= NewRank; END IF;
  IF NOT(Addition IS NULL) THEN SET @rank:= @rank + Addition; END IF;
  RETURN @rank;   
END

Next up, I changed the ZQuery1 to read something like:

select ranking(null,1) as rank
  ,groep.*
  from groep
join (select ranking(0,null)) r

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.

你是暖光i 2024-11-07 22:41:23

这是不行的,只能参数化值。
您可以做的最好的事情是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文