【问题标题】:cursor within a trigger触发器内的光标
【发布时间】:2020-05-17 20:52:00
【问题描述】:

我正在尝试在触发器中创建游标,游标的结果允许我在另一个表中进行插入,但我的 NEW.id 有问题,我认为当我输入游标时,此参数变为空..它怎么能解决它?

当我发送静态数据时,它可以正常工作

感谢您的帮助。

 BEGIN          
             DECLARE finished INTEGER DEFAULT 0;                        
             DECLARE _ingredient_id INTEGER;            
             DECLARE _unit_cost decimal(11,2);
             DECLARE _quantity INTEGER;                                                         

            DECLARE items CURSOR FOR
            SELECT ingredients.id,ingredients.unit_cost,recipes.quantity 
            FROM sales, orders,detail_orders,recipes,ingredients 
            WHERE sales.id = NEW.id 
            AND orders.sale_id= NEW.id 
            AND detail_orders.order_id=orders.id            
            AND detail_orders.food_id=recipes.food_id           
            AND recipes.ingredient_id=ingredients.id;
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;            

            OPEN items;            
             getItems: LOOP
            FETCH items INTO _ingredient_id,_unit_cost,_quantity;            
             IF finished = 1 THEN                               
                LEAVE getItems;             
             END IF;            

              SET @total= _unit_cost*_quantity;            
             INSERT INTO inventories (type,date,ingredient_id,quantity,unit_cost,total,sale_id) VALUES (2,now(),_ingredient_id,_quantity,_unit_cost,@total,NEW.id);                  

             END LOOP getItems;  
             CLOSE items;    
END

【问题讨论】:

  • 您能否添加表和数据,并插入或更新您使用的,以免重现您的问题
  • 你为什么首先使用光标?您的 select + insert 可以替换为单个 insert ... select ... 查询。这是什么类型的触发器?插入之前还是之后?
  • 感谢您的回答,我设法通过程序解决了问题

标签: mysql triggers cursor


【解决方案1】:

我设法用存储过程解决了这个问题

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多