试过多个StackOverflow的回答都不行,偶然发现一篇文章可以用。
脚本
insert into [dbo].[tags] ([post_id], [tag])
select * from (
values (10, 'tag123') -- sample value
) as s([post_id], [tag])
where not exists (
select * from [dbo].[tags] t with (updlock)
where s.[post_id] = t.[post_id] and s.[tag] = t.[tag]
)