0

我想将临时表中的数据插入到原始表中

结构相同,但有 3 个这样的键

table original 
{NIP_SPV  nvarchar (10),
NIP_SUB nvarchar (10),
TransActionDate date,
...
}

我想在数据不存在的地方插入数据

我的代码看起来像这样

IF NOT EXISTS ( 
   SELECT * FROM table_original a Inner Join table_temp b  
   on a.transactiondate = b.transactiondate and a.nip_spv = b.nip_spv 
   and a.nip_sub = b.nip_sub )  
      Begin 
        INSERT INTO T_EmployeeGroup 
        select nip_spv,nip_sub,spv_usertype,sub_usertype,appr_year  
        from table_temp
      END
 Else 
    Begin
      Update  A
      A.column_n =B.column_n
      from table_original A
      Inner JOIN table_temp B 
      on  a.transactiondate = b.transactiondate and a.nip_spv = b.nip_spv 
      and a.nip_sub = b.nip_sub
    end

案例是

when i insert data for the second time is failed
 first case i insert 10 data
 second case i insert 20 data(10 old data from first case)

我未能从新数据中插入 10 个数据。

我错过了什么?

4

1 回答 1

2

您可以OUTER JOIN为此使用 an 并过滤空值:

INSERT INTO table_original 
SELECT b.* FROM table_temp b
RIGHT JOIN table_original a ON a.transactiondate = b.transactiondate
                          AND a.nip_spv = b.nip_spv 
                          AND a.nip_sub = b.nip_sub
WHERE b.transactiondate IS NULL
  AND b.nip_spv IS NULL
  AND b.nip_sub IS NULL
于 2013-09-16T12:52:50.327 回答