使用带有触发器的单个storedprocedure,使用一个主表的值插入两个不同的表

时间:2014-12-26 09:48:24

标签: sql stored-procedures triggers

让我简单解释一下,我在下面的栏目中描述了三个不同的表: -

Table1:- tbl_asset
        ID 
        ItemID 
        ItemQuantity
        ItemLocation
        ItemSubLocation
        UnitPrice

Table2:- tbl_stock
        ID 
        AssetID
        ItemID
        ItemQuantity
        StockType
        StockMode

Table3:- tbl_profile
        ID
        StockID
        ProfileID
        ItemID
        CreatedBy
        CreatedDate


如何使用表“tbl_asset”的单个插入过程插入所有表的值(可能与触发器一起使用) 有谁可以帮助我?

1 个答案:

答案 0 :(得分:0)

尝试此问题,如果有任何问题或任何困惑,请回复我

DROP TRIGGER IF EXISTS NAME_OF_TRIGGER;

CREATE TRIGGER NAME_OF_TRIGGER 
AFTER INSERT ON tbl_asset 
FOR EACH ROW BEGIN

        INSERT INTO tbl_stock(AssetID,ItemID,ItemQuantity,StockType,StockMode)
        SELECT ID,ItemID,ItemQuantity,"StockType","StockMode"
        FROM tbl_asset 
        WHERE ID=NEW.ID;        
END;

DROP TRIGGER IF EXISTS NAME_OF_TRIGGER;

CREATE TRIGGER NAME_OF_TRIGGER 
AFTER INSERT ON tbl_stock 
FOR EACH ROW BEGIN

        INSERT INTO tbl_profile(StockID,ItemID,ProfileID,CreatedBy,CreatedDate)
        SELECT ID,ItemID,"ProfileID","CreatedBy","CreatedDate"
        FROM tbl_stock 
        WHERE ID=NEW.ID       
END;