如何判断A表中的几个字段的数据在B表中存不存在
比如:A表中有月金额,合同编号,B表中有月金额,合同编号.
B表的月金额,合同编号要判断在A表中存在,才让他们insert,不存在就不让他们insert.请给实际sql语句,解决万上给分.谢谢.

解决方案 »

  1.   

    merge into OD041_TEMP b
    using a
    on(b.a= a.a and b.b = a.b ...)
    when not  matched then
    insert (b.a,b.b)
     values(a.a,a.b)   
      

  2.   

    要插入的数据为 月金额MP 合同编号为C_ID
    insert into B(月金额,合同编号)
    select 月金额,合同编号
    from A
    where A.月金额=MP  and A.合同编号= C_ID;
      

  3.   

    insert into finance_temp1(amount,contract_id)
    select a.amount,a.contract_id from finance_temp a,finance_temp1 b
    where a.amount=b.amount and a.contract_id=b.contract_idfinance_temp1为你说的b,finance_temp为你所谓的a.楼主的需求说的有些绕,以上语句实现的是:b表中的两个字段的数据,判断在a表中存在的时候,插入b表(要插入其他字段的话楼主自己修改)
      

  4.   


    insert into B(monthMoney,sid,column1,column2,.....)values(mm1,sidValue,columnValue1,columnValue2,......)
    where exists(select 1 from A where A.monthMoney=mm1 and A.sid=sidValue)其中mm1,sidValue为insert的值。
      

  5.   

    --merge into合适
    merge into b using a on(a.合同编号=b.合同编号 and a.月金额=b.月金额)
    when not matched then
    insert(b.合同编号,b.月金额) value(a.合同编号,a.月金额)
      

  6.   

    补充说明 merge into 要10g以上