insert into t1 select 137,t2.name,t2.xxx,t2.yyy from t2 where t2.id=137;
这样写的话如果t2中不存在id=137的记录的话t2中就不会插入
如何当不存在时也插入t1?t_stock库存表,t_order_mx是出库明细表
order_id=3在其中有三条记录,即一次出库了三种物品,现需要回退t_stock中这三条记录
以下选出的三条记录需要a.num=a.num+b.order_num,请问如何写update语句?
select a.num,b.order_num from t_stock a,t_order_mx b where b.stock_id=a.id and b.order_id=3;

解决方案 »

  1.   

    1.没明白,不存在时你插入什么呀,是只insert一个137,'',''的记录吗?
    2.写的也没太看明白
      
      

  2.   


    1.是的
    2.就是通过b表更新a表中3调记录,不用cursor怎么写?
      

  3.   

    1.insert into t1
        select a.id, t2.name, t2.xxx, t2.yyy
          from (select 137 id from dual) a, t2
         where a.id = t2.id(+)2.没看明白,能给出数据吗?
      

  4.   

    t_stock库存表,t_order_mx是出库明细表  
    order_id=3在其中有三条记录,即一次出库了三种物品,现需要回退t_stock中这三条记录  
    以下选出的三条记录需要a.num=a.num+b.order_num,请问如何写update语句?  t_stock表
    id num
    1  10
    2  4
    3  5
    4  20
    5  4t_order_mx表
    order_id stock id order_num
    3        1        3
    3        2        1
    3        4        2选出order_id=3的记录对应的order_num,回退对应库存表的num
      

  5.   

    回退之后t_stock
    id num
    1  13
    2  4
    3  5
    4  22
    5  4
      

  6.   

    我感觉应该使用触发器比较合理,
    在t_order_mx删除这三条数据的时候,去直接更新t_stock表,如果失败,t_stock表也不会受影响create or replace trigger TR_TEST
    after delete
    on t_order_mx
    for each rowbegin
      update t_stock set num = num + :old.order_num 
      where  id= :old.stock_idend;