简体   繁体   中英

MySQL Stored Procedure CREATE Error

I have about 2 years of mySQL under my belt but am diving into stored procedures for the first time to create an internal analytic tool for my site. I am usually good at tracking down SQL errors but this one is eluding me.

The error I get is this:

1064 - 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 '' at line 3

This is the code I execute to receive this error:

CREATE PROCEDURE STORE_ANALYTICS (IN chain VARCHAR(300))
BEGIN
    UPDATE _analytics_clicks 
    SET chainCount = (chainCount + 1) 
    WHERE visitChain = chain;

    IF SELECT ROW_COUNT() = 0 THEN
        INSERT INTO _analytics_clicks (visitChain, chainCount) 
        VALUES (chain, 1);
    END IF;
END|

It is worth noting that before I execute this, I executed

DELIMITER |

The structure of the table I am trying to alter is this:

chainID int(11)        auto_increment
visitChain  varchar(300)    
chainCount  int(11)     

When I execute line 3 by itself, replacing visitChain=chain with visitChain='0' (0 is a test chain i enetered), the command runs fine and chainCount is incremented.

Any ideas on why I am getting this error/the stored procedure is not being created?

Thanks, Matt

EDIT:

Included delimiter in SQL command:

DELIMITER |
CREATE PROCEDURE STORE_ANALYTICS (IN chain VARCHAR(300))
BEGIN
UPDATE _analytics_clicks SET chainCount = (chainCount+1) WHERE visitChain=chain;
IF ROW_COUNT() = 0 THEN
INSERT INTO _analytics_clicks (visitChain, chainCount) VALUES (chain, 1);
END IF;
END|
DELIMITER ;

Gave me this error: Fatal error: Maximum execution time of 300 seconds exceeded in C:\wamp\apps\phpmyadmin3.2.0.1\libraries\import\sql.php on line 119

You can do

IF ROW_COUNT() = 0 THEN

rather than IF SELECT ROW_COUNT() = 0 THEN . No need for the SELECT .

I suspect that your delimiter is not being set properly.

I usually include the delimiter set/unset statements with my SQL source, that way "it just runs" - it's good practice that every SQL file can run stand-alone eg:

delimiter ~

create procedure ...

end;~

delimiter ;

Try changing your delimiter to something less deadly than a pipe char - try my tilda ~ char, which never gives me any trouble. I've seen $$ used often as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM