Mysql存储过程错误1064

时间:2012-08-05 12:32:25

标签: mysql stored-procedures

我正在处理一些代码,并且遇到了这个存储过程问题...现在花了很长时间看着它,所以如果有人能告诉我这是一个菜鸟的话,那就请。

我收到以下错误:

  

错误代码:1064   您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'@ownerid varchar(50)附近使用正确的语法;       声明@active tinyint;       声明@subuser tinyint;       设置'第4行

关于此代码:

    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 ;

是存储过程的新手,所以它非常简单..... 提前致谢

2 个答案:

答案 0 :(得分:1)

你应该使用':='而不是' ='当你想在SELECT语句中将值传递给变量时,否则MySQL只会与操作数进行比较。

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

您也可以使用SELECT INTO语句,例如 -

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

SELECT syntax

答案 1 :(得分:0)

可能是失踪的@

DECLARE cardid VARCHAR(50);

...

DECLARE @cardid VARCHAR(50);