MySQL 一个过程中的多个命令
MySQL Multiple Commands in a Procedure
对于 class 作业,我需要将记录从 Checked_Out_Media table 移动到历史记录 table,其中 table 是相同的.然后我需要从 Checked_Out_Media Table 中删除记录。我已经把它归结为:
Create Procedure spMoveToHistory( IN in_UserID char(6), IN in_MediaBarcode char(3) )
BEGIN
SELECT @NextRow := (MAX(History_TUID) + 1)
From History;
Insert into History
Select @NextRow, COM.User_ID, COM.Media_Barcode,
COM.Checked_Out_Date, COM.Return_Date
From Checked_Out_Media COM
Where COM.User_ID = in_UserID AND
COM.Media_Barcode = in_MediaBarcode;
END;
在我 运行 之前,上面的代码似乎工作得很好。它没有任何作用,无论好坏。当我在创建过程后检查它时,它显示在 End 语句处有错误。我不确定为什么只有在我创建它之后才会出现...无论哪种方式,我都希望有人能澄清为什么上面的代码不起作用,所以我可以尝试添加下面的代码。 另外,这个post的主要原因是想问:是否可以在一个过程中实现多个事务?
Delete From checked_out_media
Where User_ID = in_UserID AND Media_Barcode = in_MediaBarcode;
您的存储过程的问题在于命令之间的分隔方式。正如 mysql 关于 defining stored programs 的文档所说:
If you use the mysql client program to define a stored program
containing semicolon characters, a problem arises. By default, mysql
itself recognizes the semicolon as a statement delimiter, so you must
redefine the delimiter temporarily to cause mysql to pass the entire
stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The
following example shows how to do this for the dorepeat() procedure
just shown. The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
mysql> delimiter //
mysql> CREATE PROCEDURE dorepeat(p1 INT)
-> BEGIN
-> SET @x = 0;
-> REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
-> END
-> // Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
关于您的第二个问题:您没有在存储过程中执行任何事务,您的问题是关于您是否可以在存储过程中执行多个 sql 命令。答案是肯定的,你可以执行多个sql命令,见上面的示例代码。
实际上,您可能想考虑将插入和删除包含在一个事务中,但这是一个不同的问题。
对于 class 作业,我需要将记录从 Checked_Out_Media table 移动到历史记录 table,其中 table 是相同的.然后我需要从 Checked_Out_Media Table 中删除记录。我已经把它归结为:
Create Procedure spMoveToHistory( IN in_UserID char(6), IN in_MediaBarcode char(3) )
BEGIN
SELECT @NextRow := (MAX(History_TUID) + 1)
From History;
Insert into History
Select @NextRow, COM.User_ID, COM.Media_Barcode,
COM.Checked_Out_Date, COM.Return_Date
From Checked_Out_Media COM
Where COM.User_ID = in_UserID AND
COM.Media_Barcode = in_MediaBarcode;
END;
在我 运行 之前,上面的代码似乎工作得很好。它没有任何作用,无论好坏。当我在创建过程后检查它时,它显示在 End 语句处有错误。我不确定为什么只有在我创建它之后才会出现...无论哪种方式,我都希望有人能澄清为什么上面的代码不起作用,所以我可以尝试添加下面的代码。 另外,这个post的主要原因是想问:是否可以在一个过程中实现多个事务?
Delete From checked_out_media
Where User_ID = in_UserID AND Media_Barcode = in_MediaBarcode;
您的存储过程的问题在于命令之间的分隔方式。正如 mysql 关于 defining stored programs 的文档所说:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The following example shows how to do this for the dorepeat() procedure just shown. The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.
mysql> delimiter // mysql> CREATE PROCEDURE dorepeat(p1 INT) -> BEGIN -> SET @x = 0; -> REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT; -> END -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ;
关于您的第二个问题:您没有在存储过程中执行任何事务,您的问题是关于您是否可以在存储过程中执行多个 sql 命令。答案是肯定的,你可以执行多个sql命令,见上面的示例代码。
实际上,您可能想考虑将插入和删除包含在一个事务中,但这是一个不同的问题。