简体   繁体   English

mysql 存储过程 - 选择…进行更新

[英]mysql stored procedure - select… for update

i'm using "select... for update" in a stored procedure.我在存储过程中使用“选择...进行更新”。 but when the stored procedure finishes it returns the result from the "select... for update" i did at the beginning.但是当存储过程完成时,它会返回我在开始时所做的“选择...更新”的结果。 is there a way to make it block the rows without returning them?有没有办法让它阻止行而不返回它们?

stored procedure code:存储过程代码:

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_save`(
in param_cod_emp char(5),
in param_cod_tab char(5),
in param_des_tab varchar(45),
in param_flg_new char(1))
BEGIN

declare var_cod_tab char(5);

start transaction;

select *
from tabla
where cod_emp=param_cod_emp
for update;

if(param_flg_new='0') then

set var_cod_tab=
(select max(cod_tab)+1
from tabla
where cod_emp=param_cod_emp);

insert into tabla(
cod_emp,
cod_tab,
des_tab)
values(
param_cod_emp,
var_cod_tab,
param_des_tab);

else

udpate tabla where
des_tab=param_des_tab
where cod_emp=param_cod_emp
and cod_tab=param_cod_tab;

end if;

commit;

END

is this code a good way to make a char column look like an auto_increment?这段代码是使 char 列看起来像 auto_increment 的好方法吗? thanks very much for your time.非常感谢你花时间陪伴。

You can LOCK TABLE (MyISAM tables) or BEGIN TRANSACTION or what's the syntax (for transaction-capable table engines like InnoDB or Maria).您可以LOCK TABLE (MyISAM 表)或BEGIN TRANSACTION或语法是什么(对于具有事务能力的表引擎,如 InnoDB 或 Maria)。

Update: Oh, now I see you do START TRANSACTION.更新:哦,现在我看到你开始交易了。 Well, then:好吧:

MySQL Stored Procedures return the result of the last SELECT performed. MySQL 存储过程返回最后执行的 SELECT 的结果。 So if you want to return other resultset, just do another SELECT.因此,如果您想返回其他结果集,只需执行另一个 SELECT。

if you use a trigger and a sequence to increment and insert the id then youd never have to select max() from the table you are inserting or updating from and probably wouldnt have this problem, ive never used a select for update tho..如果您使用触发器和序列来增加和插入 id,那么您永远不必从您要插入或更新的表中使用 select max() 并且可能不会有这个问题,我从来没有使用过 select 来更新。

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

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