MySQL 在mySql中创建一个带参数的过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5039324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:49:36  来源:igfitidea点击:

Creating a procedure in mySql with parameters

mysqlparametersprocedure

提问by Peter Rasmussen

I am trying to make a stored procedure using mySQL. This procedure will validate a username and a password. I'm currently running mySQL 5.0.32 so it should be possible to create procedures.

我正在尝试使用 mySQL 创建一个存储过程。此过程将验证用户名和密码。我目前正在运行 mySQL 5.0.32,因此应该可以创建过程。

Heres the code I've used. All I get is an SQL syntax error.

这是我使用过的代码。我得到的只是一个 SQL 语法错误。

   GO
    CREATE PROCEDURE checkUser
    (IN @brugernavn varchar(64)),IN @password varchar(64))
    BEGIN
    SELECT COUNT(*)  FROM bruger WHERE bruger.brugernavn=@brugernavn AND bruger.pass=@Password;
    END;

Thank you in advance

先感谢您

回答by Peter Rasmussen

I figured it out now. Here's the correct answer

我现在想通了。这是正确答案

CREATE PROCEDURE checkUser 
(
   brugernavn1 varchar(64),
   password varchar(64)
) 
BEGIN 
   SELECT COUNT(*) FROM bruger 
   WHERE bruger.brugernavn=brugernavn1 
   AND bruger.pass=password; 
END; 

@ points to a global var in mysql. The above syntax is correct.

@ 指向 mysql 中的全局变量。上面的语法是正确的。

回答by Alexo

(IN @brugernavn varchar(64)**)**,IN @password varchar(64))

The problem is the )

问题是 )

回答by Brajesh

Its very easy to create procedure in Mysql. Here, in my example I am going to create a procedure which is responsible to fetch all data from student table according to supplied name.

在Mysql中创建过程非常容易。在这里,在我的示例中,我将创建一个过程,该过程负责根据提供的名称从学生表中获取所有数据。

DELIMITER //
CREATE PROCEDURE getStudentInfo(IN s_name VARCHAR(64))
BEGIN
SELECT * FROM student_database.student s where s.sname = s_name;
END//
DELIMITER;

In the above example ,database and table names are student_database and student respectively. Note: Instead of s_name, you can also pass @s_name as global variable.

在上面的例子中,数据库名和表名分别是 student_database 和 student。注意:除了 s_name,您还可以将 @s_name 作为全局变量传递。

How to call procedure? Well! its very easy, simply you can call procedure by hitting this command

如何调用程序?好!它非常简单,只需点击此命令即可调用过程

$mysql> CAll getStudentInfo('pass_required_name');

enter image description here

在此处输入图片说明