当我从java代码插入记录时mysql触发器不起作用

发布于 2024-11-01 08:34:01 字数 969 浏览 0 评论 0原文

DELIMITER //

DROP TRIGGER insert_orderinward_trig; //

CREATE TRIGGER insert_orderinward_trig AFTER INSERT ON tblm_tlmngorderinward FOR EACH ROW
BEGIN

    call temp_proc(NEW.itemcode);

END; //

我编写程序,因为

DELIMITER //

DROP PROCEDURE temp_proc; //

CREATE PROCEDURE temp_proc(IN code VARCHAR(80))

BEGIN
    DECLARE inwardstock,issuestock,updatestock DECIMAL(15,0) DEFAULT 0;

    SET inwardstock = (SELECT SUM(stock) FROM tblm_tlmngorderinward WHERE itemcode = code);
    IF(@inwardstock > 0) THEN
        SET updatestock =inwardstock;   
    END IF;


    SET issuestock = (SELECT SUM(stock) FROM tblt_tlmngissueitem WHERE itemcode = code);

    IF(@issuestock > 0) THEN
        SET updatestock = @updatestock-@issuestock ;    
    END IF;

    UPDATE tblm_tlmngitem SET stock=updatestock  WHERE itemcode=code;

END; //

DELIMITER ;

当我手动插入记录时,我的插入触发器工作正常。但是当我在java中使用preparestatement插入记录时它无法工作。

DELIMITER //

DROP TRIGGER insert_orderinward_trig; //

CREATE TRIGGER insert_orderinward_trig AFTER INSERT ON tblm_tlmngorderinward FOR EACH ROW
BEGIN

    call temp_proc(NEW.itemcode);

END; //

I write procedure as

DELIMITER //

DROP PROCEDURE temp_proc; //

CREATE PROCEDURE temp_proc(IN code VARCHAR(80))

BEGIN
    DECLARE inwardstock,issuestock,updatestock DECIMAL(15,0) DEFAULT 0;

    SET inwardstock = (SELECT SUM(stock) FROM tblm_tlmngorderinward WHERE itemcode = code);
    IF(@inwardstock > 0) THEN
        SET updatestock =inwardstock;   
    END IF;


    SET issuestock = (SELECT SUM(stock) FROM tblt_tlmngissueitem WHERE itemcode = code);

    IF(@issuestock > 0) THEN
        SET updatestock = @updatestock-@issuestock ;    
    END IF;

    UPDATE tblm_tlmngitem SET stock=updatestock  WHERE itemcode=code;

END; //

DELIMITER ;

My insert trigger work fine when i manually insert record. But it can not work when i insert record using preparestatement in java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-11-08 08:34:01

乍一看,您似乎有两个变量 inwardstock/@inwardstock 和 issuesstock/@issuestock ,这看起来是错误的:

DECLARE inwardstock,issuestock,updatestock DECIMAL(15,0) DEFAULT 0;

IF(@inwardstock > 0) THEN <--- @inwardstock or inwardstock !!

...

IF(@issuestock > 0) THEN <--- @issuestock or issuestock !!

我的另一个建议是完全放弃触发器并只调用存储过程:

delimiter #

create procedure insert_tblm_tlmngorderinward
(
in p_itemcode varchar(80) -- p_ is for param
)
begin

-- v_ is for variable 

declare v_inwardstock, v_issuestock, v_updatestock decimal(15,0) default 0;

    -- do this in stored proc

    insert into tblm_tlmngorderinward (itemcode) values (p_itemcode);

    -- now do all the trigger stuff

    select sum(stock) into v_inwardstock from tblm_tlmngorderinward where itemcode = p_itemcode;
    select sum(stock) into v_issuestock from tblt_tlmngissueitem where itemcode = p_itemcode;

    if(v_inwardstock > 0) then
        set v_updatestock = v_inwardstock;      
    end if;

    if(v_issuestock > 0) then
        set v_updatestock = v_updatestock - v_issuestock;       
    end if;

    update tblm_tlmngitem set stock = v_updatestock where itemcode = p_itemcode;

end#

delimiter ;

call insert_tblm_tlmngorderinward('why_use_a_trigger');

希望这会有所帮助:)

On first glance you seem to have 2 variables for inwardstock/@inwardstock and issuestock/@issuestock which looks wrong:

DECLARE inwardstock,issuestock,updatestock DECIMAL(15,0) DEFAULT 0;

IF(@inwardstock > 0) THEN <--- @inwardstock or inwardstock !!

...

IF(@issuestock > 0) THEN <--- @issuestock or issuestock !!

The other suggestion I have is to drop the trigger completely and just call a stored proc:

delimiter #

create procedure insert_tblm_tlmngorderinward
(
in p_itemcode varchar(80) -- p_ is for param
)
begin

-- v_ is for variable 

declare v_inwardstock, v_issuestock, v_updatestock decimal(15,0) default 0;

    -- do this in stored proc

    insert into tblm_tlmngorderinward (itemcode) values (p_itemcode);

    -- now do all the trigger stuff

    select sum(stock) into v_inwardstock from tblm_tlmngorderinward where itemcode = p_itemcode;
    select sum(stock) into v_issuestock from tblt_tlmngissueitem where itemcode = p_itemcode;

    if(v_inwardstock > 0) then
        set v_updatestock = v_inwardstock;      
    end if;

    if(v_issuestock > 0) then
        set v_updatestock = v_updatestock - v_issuestock;       
    end if;

    update tblm_tlmngitem set stock = v_updatestock where itemcode = p_itemcode;

end#

delimiter ;

call insert_tblm_tlmngorderinward('why_use_a_trigger');

Hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文