INSERT INTO myTable(columns...) VALUES(values...) WHERE NOT EXISTS (SELECT * FROM myTable WHERE pk_part1 = value1,AND pk_part2 = value2)
我该如何做到这一点?
(一般来说,可以将insert与where子句组合起来)
INSERT INTO myTable(columns...) Select values... WHERE NOT EXISTS (SELECT * FROM myTable WHERE pk_part1 = value1,AND pk_part2 = value2)
编辑:阅读马丁后链接,如果承认,最好的解决办法是:
BEGIN TRY INSERT INTO myTable(columns...) values( values...) END TRY BEGIN CATCH IF ERROR_NUMBER() <> 2627 RAISERROR etc END CATCH;