简体   繁体   中英

MySQL Stored Procedure syntax error with table variable

I have this MySQL query to create a stored procedure:

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//

but I'm keep getting this error - can't understand why:

#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

Change #1

Delimiter //; to Delimiter //

Change #2

Create a Temp Table in Memory

Change #3

Changed

Select Inviting_Id 
From Invited_Users
Where Invited_Id = facebookId
Into invitingUsers;

into

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

With these changes I give you this:

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 ;

Give it a Try !!!

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