简体   繁体   中英

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? 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).

Update: Oh, now I see you do START TRANSACTION. Well, then:

MySQL Stored Procedures return the result of the last SELECT performed. So if you want to return other resultset, just do another 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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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