在存储过程中准备语句时出现语法错误

时间:2016-01-27 14:58:51

标签: mysql stored-procedures prepare mysql-5.6

在Android游戏中,用户可以通过Google+,Facebook,Twitter登录:

app screenshot

当应用程序连接到MySQL / PHP后端时,它会发送一个社交ID列表,并将其存储为社交表中的 sid 列:

+--------+---------------+------+-----+---------+-------+
| Field  | Type          | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| sid    | varchar(180)  | NO   | PRI | NULL    |       |
| given  | varchar(180)  | NO   |     | NULL    |       |
| family | varchar(180)  | YES  |     | NULL    |       |
| city   | varchar(180)  | YES  |     | NULL    |       |
| photo  | varchar(1000) | YES  |     | NULL    |       |
| stamp  | int(11)       | NO   |     | NULL    |       |
| uid    | int(11)       | NO   |     | NULL    |       |
+--------+---------------+------+-----+---------+-------+

在另一个名为用户的表格中,我将自动增量用户ID保留为 uid 列,并使用它来跟踪他们的游戏和成就(列奖牌 ):

+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| uid           | int(11)      | NO   | PRI | NULL    | auto_increment |
| created       | int(11)      | NO   |     | NULL    |                |
| stamp         | int(11)      | NO   |     | NULL    |                |
| banned_until  | datetime     | YES  |     | NULL    |                |
| banned_reason | varchar(180) | YES  |     | NULL    |                |
| medals        | int(11)      | NO   |     | 0       |                |
+---------------+--------------+------+-----+---------+----------------+

在我的PHP登录脚本中,我尝试尽可能合并社交帐户 -

通过接收所有收到的 sids ,找到相应的 uids ,然后取最低找到 uid 并更新社交表中的记录使用最低 uid

这在PHP中运行良好,但现在我尝试将合并功能移到带有prepared statements的MySQL存储过程中:

delimiter $$$

drop procedure if exists merge_users;
create procedure merge_users(IN in_sids varchar(255))
begin
        declare sql_1 varchar(255);
        declare sql_2 varchar(255);
        declare out_uid varchar(255);

        set sql_1 = concat('select min(uid) into out_uid from social where sid in (', in_sids, ')');
        prepare sth_1 from sql_1;
        execute sth_1;
        deallocate prepare sth_1;

        IF found_rows() > 0 THEN

                set sql_2 = concat('update social set uid=', out_uid, ' where sid in (', in_sids, ')');
                prepare sth_2 from sql_2;
                execute sth_2;
                deallocate prepare sth_2;
        ELSE
                insert into users(created, stamp, medals, green, red)
                values (unix_timestamp(), unix_timestamp(), 0, 0, 0);

                select last_insert_uid() into out_uid;

        END IF;

        select out_uid;
end
$$$

不幸的是,上面的代码会输出语法错误消息:

ERROR 1064 (42000): You have an error in your SQL syntax; 
                    check the manual that
        execute sth_1;
        deallocate prepare sth_1;

        IF foun' at line 8

0 个答案:

没有答案