简体   繁体   中英

Mysql stored procedure error 1064

I'm working on some code, and got stuck on this stored procedure problem... Have spent too long now looking at it, so please if someone can tell me where I am a noob with this.

I get the following error:

Error Code: 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 '@ownerid varchar(50); declare @active tinyint; declare @subuser tinyint; set ' at line 4

on this code:

    DELIMITER //

USE `test`//

DROP PROCEDURE IF EXISTS `getAccount`//

CREATE DEFINER=`peter`@`%` PROCEDURE `getAccount`( IN cid VARCHAR(20))
BEGIN
    DECLARE cardid VARCHAR(50);
    DECLARE @ownerid VARCHAR(50);
    DECLARE @active TINYINT;
    DECLARE @subuser TINYINT;
    SET cardid = MD5( cid + SHA1(cid + 'a salt value'));

    SELECT @active = active, @ownerid = ownerid, @subuser = subuser FROM cards_tbl WHERE cardhash = cardid;

    IF @active = 1 THEN
        IF @subuser = 1 THEN
            SELECT subuser_m2s_tbl.name,subuser_m2s_tbl.image, user_saldo.saldo AS credits FROM subuser_m2s_tbl 
            JOIN user_saldo ON subuser_m2s_tbl.subhash = user_saldo.userhash 
            WHERE subuser_m2s_tbl.subhash = @ownerid;
        ELSE
            SELECT user_m2s_tbl.name,user_m2s_tbl.image, user_saldo.saldo AS credits FROM user_m2s_tbl 
            JOIN user_saldo ON user_m2s_tbl.userhash = user_saldo.userhash 
            WHERE user_m2s_tbl.userhash = @ownerid; 
        END IF;
    END IF; 
    END$$

DELIMITER ;

Am new to the stored procedures, so it is properly something very simple..... Thanks in advance

You should use ':=' instead of '=' when you want to pass value into variable in SELECT statement, otherwise MySQL just will compare to operands.

SELECT
  active_var := active, ownerid_var := ownerid, subuser_var := subuser
FROM
  cards_tbl
WHERE
  cardhash = cardid;

Also you can use SELECT INTO statement, eg -

SELECT
  active, ownerid, subuser
INTO
  active_var, ownerid_var, subuser_var
FROM
  cards_tbl
WHERE
  cardhash = cardid;

SELECT syntax .

Is it maybe the missing @

DECLARE cardid VARCHAR(50);

...

DECLARE @cardid VARCHAR(50);

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