带有表变量的MySQL存储过程语法错误

时间:2013-07-16 16:52:40

标签: mysql stored-procedures

我有这个MySQL查询来创建存储过程:

Delimiter //;
Create Procedure addUser(
    IN facebookId varchar(20), 
        IN name varchar(50), 
        In accessToken varchar(100), 
        in expires float)
Begin
    Declare invitingUsers Table(Id varchar(20));

        Insert Into Users 
        (`Facebook_Id`,`Name`,`Access_Token`,`Expired`) Values (facebookId,name,accessToken,expires);

        Select Inviting_Id 
        From Invited_Users
        Where Invited_Id = facebookId
        Into invitingUsers;

        Update Table Users 
        Set Credit = Credit + 1 
        Where Facebook_Id In (Select Id From invitingUsers);
End//

但我一直收到这个错误 - 无法理解原因:

#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 'Table(Id varchar(20)); Insert Into Users (`Facebook' at line 7

1 个答案:

答案 0 :(得分:2)

更改#1

Delimiter //;Delimiter //

更改#2

在内存中创建临时表

更改#3

更改

Select Inviting_Id 
From Invited_Users
Where Invited_Id = facebookId
Into invitingUsers;

进入

INSERT INTO invitingUsers          
Select Inviting_Id From Invited_Users
Where Invited_Id = facebookId;

通过这些更改,我给你这个:

Delimiter //
Create Procedure addUser(
    IN facebookId varchar(20), 
        IN name varchar(50), 
        In accessToken varchar(100), 
        in expires float)
Begin
        Declare invitingUsers Table(Id varchar(20));

        Create temporary table if not exists invitingUsers
        (Id varchar(20), PRIMARY KEY (id)) ENGINE=MEMORY;

        Insert Into Users 
        (`Facebook_Id`,`Name`,`Access_Token`,`Expired`)
        Values (facebookId,name,accessToken,expires);

        INSERT INTO invitingUsers          
        Select Inviting_Id From Invited_Users
        Where Invited_Id = facebookId;

        Update Table Users 
        Set Credit = Credit + 1 
        Where Facebook_Id In (Select Id From invitingUsers);
End//
Delimiter ;

试一试!!!