简体   繁体   English

mysql多个存储过程更新

[英]mysql multiple stored procedure update

I'm having some trouble with mysql updateusers stored procedure that look like this: 我遇到了一些mysql updateusers存储过程的问题,如下所示:

 DELIMITER go

 Create procedure updateusers(
   IN UserID tinyint(11),
   IN FirstName varchar(30),
   IN LastName varchar(30),
   IN Password varchar(30),
   IN EmailAddress varchar(30),
   IN Salt varchar(40),
   IN RoleID varchar(1))
 BEGIN
   update  users
   set
     FirstName = FirstName
     where UserID = UserID
 End
 BEGIN
   update  users
   set
     LastName = LastName
     where UserID = UserID

 End
 BEGIN
   update  users
   set     
     Password = Password
     where UserID = UserID

 End
 BEGIN
   update  users
   set
     EmailAddress = EmailAddress
     where UserID = UserID
 End
 BEGIN
   update  users
   set
     Salt = Salt
     where UserID = UserID
 End

 BEGIN
   update  users
   set
     RoleID = RoleID
     where UserID = UserID;  
 End
 go

 DELIMITER ;

and I got an error in line 16 that says: 我在第16行得到一个错误:

MySQL said: Documentation #1064 - You have an error in your SQL syntax; MySQL说:文档#1064 - 你的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法

 'End BEGIN update users set LastName = LastName where UserID = Us' at line 16 

Then I would have used the call stored procedure like this: 然后我会使用这样的调用存储过程:

call updateusers(3,'John','Jamieson','dsd','jamie@gmail.com','abac123','U')

The output that I would like to get is for example if I want to update from: 我希望获得的输出是,例如,如果我想从以下更新:

 UserId  FirstName  LastName  Password  EmailAddress    Salt  RoleID
 3       John       Smith     abc       john@gmail.com  123   U

to this: 对此:

 UserId  FirstName  LastName  Password  EmailAddress       Salt    RoleID
 3       John       Jamieson    dsd       jamie@gmail.com  abac123   U

or 要么

UserId  FirstName  LastName  Password  EmailAddress       Salt    RoleID
 3       Aaron     Smith     abc       john@gmail.com     123     A

Admittedly, the documentation for MySQL stored procedures has never been very good. 不可否认,MySQL存储过程的文档从未如此出色。 There are not enough examples. 没有足够的例子。

In your case, I would write the procedure like this: 在你的情况下,我会写这样的程序:

 DELIMITER go

 CREATE PROCEDURE updateusers(
   IN inUserID tinyint,
   IN inFirstName varchar(30),
   IN inLastName varchar(30),
   IN inPassword varchar(30),
   IN inEmailAddress varchar(30),
   IN inSalt varchar(40),
   IN inRoleID varchar(1))
 BEGIN
   UPDATE users
   SET
     FirstName = inFirstName,
     LastName = inLastName,
     Password = inPassword,
     EmailAddress = inEmailAddress,
     Salt = inSalt,
     RoleID = inRoleID
   WHERE UserID = inUserID;
 END
 go

 DELIMITER ;

Changes: 变化:

  • Name your input parameters something distinct from the column names in your table, otherwise it's ambiguous if you want to set FirstName to the input parameter of the same name, or to itself (which would be a no-op). 将输入参数命名为与表中的列名不同的内容,否则,如果要将FirstName设置为同名的输入参数,或者将其设置为自身(这将是无操作),则它是不明确的。 To remove the ambiguity, I just used "in" as a prefix for the input parameters. 为了消除歧义,我只使用“in”作为输入参数的前缀。
  • You can update more than one column in a single UPDATE . 您可以在单个UPDATE更新多个列。 In fact, you should do this, so you only need to run one query to find the row to be updated. 实际上,您应该这样做,因此您只需要运行一个查询来查找要更新的行。
  • You don't need a BEGIN...END around every statement, just around blocks of statements, similar to how you use curly-braces in many programming languages. 你不需要在每个语句周围使用BEGIN...END ,只需要在语句块周围,类似于在许多编程语言中使用花括号的方式。
  • Terminate your UPDATE statement with a semicolon. 用分号终止UPDATE语句。
  • I changed tinyint(11) to simply tinyint . 我将tinyint(11)改为tinyint The length argument doesn't do anything. 长度参数不起作用。 Read my answer to Types in MySQL: BigInt(20) vs Int(20) 阅读我对MySQL中的类型的回答:BigInt(20)vs Int(20)

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

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