0

I want to be able to pass arguments to stored procedure, so I searched the net and encountered something like this:

DELIMITER $$
CREATE PROCEDURE addTmpUser 
@id varchar(10)
AS
BEGIN
    //some sql code
END$$

DELIMITER ; 

The problem is that I am getting a syntax error for the @ character.

Note: I am using MySQL db.

1
  • added the ; and still same error Commented Oct 25, 2013 at 19:22

2 Answers 2

2

You are mixing variable types.
@variable is a user variable with a scope for the entire connection.
The variables in stored procedures look different, they don't have the @ before them.

Also, you need to declare them. Here is an example

DELIMITER $$
CREATE PROCEDURE addTmpUser(p_id varchar(10))
-- the variable is named p_id as a nameing convention. 
-- It is easy for variables to be mixed up with column names otherwise.
BEGIN
    DECLARE innerVariable int;
    insert into user (id) values (p_id);
    -- return all users
    select * from user;
END$$

DELIMITER ; 

-- and now call it
call addTmpUser(10);
Sign up to request clarification or add additional context in comments.

4 Comments

and how should I call it? (execute the procedure)
You can do call addTmpUser(10);
what does this: DECLARE innerVariable int line do?
That declares a variable that is only seen inside the procedure.
2

You need to use IN,OUT,INOUT to specify the parameter. So you can try this

DELIMITER $$
CREATE PROCEDURE addTmpUser (IN id VARCHAR(10))
BEGIN
    //some sql code
END$$

DELIMITER ;

Look at the documentation

2 Comments

now I am getting the error on the AS word. (unexpected AS)
and how should I call it? (execute the procedure)

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.