我想将表临时数据插入表原始
结构相同但有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个数据。
我错过了什么?
答案 0 :(得分:2)
您可以使用OUTER JOIN
来过滤空值:
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