Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1017
  • Last Modified:

MySQL compound statement problem

I am a first time user for MySQL compound statements.

I figured out that this shoud be the one I need

BEGIN
    DECLARE iDone INT DEFAULT 0;
    DECLARE sUsr VARCHAR(50);
    DECLARE iMax INT(10);
    DECLARE c_Upd CURSOR FOR SELECT szUser, MAX(TotalPages) FROM test GROUP BY szUser;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET iDone = 1;

    OPEN c_Upd;

    REPEAT
        FETCH c_Upd INTO sUSR, iMax;
        IF NOT done THEN
            UPDATE Test SET MaxPages = iMax WHERE szUser = sUSR;
        END IF;
    UNTIL iDone END REPEAT;

    CLOSE c_Upd;
END

but whatever I try to change on de DECLARE line I always get

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'DECLA
RE iDone INT DEFAULT 0;
    at line 2

I have tried several things  even the simplest one

BEGIN
DECLARE iDone INT;
END

will not work.

I am using Server version: 5.1.30-community MySQL Community Server (GPL).

What do I do wrong?


0
Marc_Engrie
Asked:
Marc_Engrie
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
hernst42Commented:
Where do you run that sql? from the mysqlclient? Then try:
delimiter //
<your strored procedure code>
delimiter;
0
 
Marc_EngrieAuthor Commented:
Tried that as well but still get the same error :-(


mysql> DELIMITER //
mysql> BEGIN
    ->     DECLARE iDone INT DEFAULT 0;
    -> END
    -> //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'DECLA
RE iDone INT DEFAULT 0;
END' at line 2
mysql>


0
 
hernst42Commented:
You need to create a stored procedure or function first and then call that function. AFAIK you can't define standalone code like that and execute it.
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
khufford19Commented:
I'm just curious, and I know it's kinda a stupid question, but are you trying to setup a stored procedure, or are you just trying to execute this code as a standard query.  If it's the former, then you may need to start with the CREATE statement (see below), as the procedure probably hasn't yet been defined.  You can then use the CALL statement to actually execute the operation.

If you want to get fancy with it and return some values from or pass some values to the operation, you can definitely do that, but I'd suggest taking another look at the details available in the MySQL Reference Manual
http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
mysql> delimiter //
mysql> CREATE PROCEDURE UserPageCount
    -> BEGIN
    ->     DECLARE iDone INT DEFAULT 0;
    ->     DECLARE sUsr VARCHAR(50);
    ->     DECLARE iMax INT(10);
    ->     DECLARE c_Upd CURSOR FOR SELECT szUser, MAX(TotalPages) FROM test GROUP BY szUser;
    ->     DECLARE CONTINUE HANDLER FOR NOT FOUND SET iDone = 1;
    ->     OPEN c_Upd;
    ->     REPEAT
    ->         FETCH c_Upd INTO sUSR, iMax;
    ->         IF NOT done THEN
    ->             UPDATE Test SET MaxPages = iMax WHERE szUser = sUSR;
    ->         END IF;
    ->     UNTIL iDone END REPEAT;
    ->     CLOSE c_Upd;
    -> END;
    -> //
 
mysql> delimeter ;
 
mysql> CALL UserPageCount

Open in new window

0
 
racekCommented:
you don't need CURSOR !!! and UPDATE you table faster with one statment :-)

UPDATE Test, (SELECT szUser sUSR, MAX(TotalPages) iMax  FROM test GROUP BY szUser ) t2
 SET MaxPages = t2.iMax
 WHERE szUser = t2.sUSR;

Open in new window

0
 
Marc_EngrieAuthor Commented:
Hello,

in the meanwhile I found out that using MySQL Administrator, one can create stored procedures there. The exact same statements (inluding spaces etc) do work here. I have no clue why but, hey, it's working and that's all it takes for me right now.

Of course it would be nice to know why the stuff does not work in de command line client but that for later rightnow. No time to waste.

BTW: thx for the enhanced UPDATE statement but that was not the real problem/question.
0
 
racekCommented:
have you test my query - withou stored procedure and without CURSOR ???

UPDATE Test, (SELECT szUser sUSR, MAX(TotalPages) iMax
                            FROM test GROUP BY szUser ) t2
 SET MaxPages = t2.iMax
 WHERE szUser = t2.sUSR;
0
 
Marc_EngrieAuthor Commented:
Query works!
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now