3

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.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Johan
  • 74,508
  • 24
  • 191
  • 319

5 Answers5

2

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.

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
  • That will still give an error as soon as I make the query active. Or an I missing something. BTW I'm not bothered about the speed of preparing queries, the dataset this query works on is always small. – Johan Apr 11 '11 at 19:44
1

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

  • Yep tried that, but the code is horribly complex and my patches do not seem to do anything. – Johan Apr 11 '11 at 20:17
  • +1 really is a good idea, if only I was smart/patient enough to figure the ZEOS code out. – Johan Apr 12 '11 at 15:15
0

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 `` .

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • The query is actually in Dutch, I translated stuff for the benefit of the general public, did not realize I'd get a conflict with group, will fix the code back to Dutch :-) – Johan Apr 11 '11 at 19:47
  • Anyway that was not the problem, just that the orig query was **long**, so I condensed it. – Johan Apr 11 '11 at 19:50
0

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

robmil
  • 312
  • 2
  • 4
  • I think this is due to ZEOS, without ParamCheck it just skips params it facts it then fails on a param, but it still does not grok @x:= @y. – Johan Apr 11 '11 at 21:10
0

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.

Johan
  • 74,508
  • 24
  • 191
  • 319