1

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
1
  • 1
    apparently mySql doesn't use table variable. the solution was to create a temp table Commented Jul 16, 2013 at 17:23

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.