简体   繁体   English

MySQL存储过程@语法错误

[英]MySQL stored procedure @ syntax error

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. 注意:我正在使用MySQL数据库。

You need to use IN , OUT , INOUT to specify the parameter. 您需要使用INOUTINOUT来指定参数。 So you can try this 所以你可以试试看

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

DELIMITER ;

Look at the documentation 查看文档

You are mixing variable types. 您正在混合变量类型。
@variable is a user variable with a scope for the entire connection. @variable是一个用户变量,具有整个连接的作用域。
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM