mysql 存储过程 - 选择...进行更新
我在存储过程中使用“选择...进行更新”。 但是当存储过程完成时,它会返回我一开始所做的“选择...进行更新”的结果。有没有办法让它阻塞行而不返回它们?
存储过程代码:
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
这段代码是使 char 列看起来像 auto_increment 的好方法吗? 非常感谢您的宝贵时间。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以
LOCK TABLE
(MyISAM 表)或BEGIN TRANSACTION
或语法是什么(对于支持事务的表引擎,如InnoDB 或Maria)。更新:哦,现在我看到你开始交易了。那么:
MySQL 存储过程返回最后执行的 SELECT 的结果。所以如果你想返回其他结果集,只需再执行一次 SELECT 即可。
You can
LOCK TABLE
(MyISAM tables) orBEGIN 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.
如果您使用触发器和序列来递增和插入 id,那么您永远不必从要插入或更新的表中选择 max() 并且可能不会出现此问题,我从未使用过 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..