我想向表插入一条记录,但如果表已经存在此记录,就不插入,不知如何实现?
insert into tb(Bh) values('001');
如果表里存在Bh=001的记录,就不插入,不知如何做到这点?
我现在是先判断select * from tb where Bh<>'001'
后再执行inser into语句,
觉得有点麻烦,不知能不能用一句SQL来实现?谢谢
------解决方案--------------------
虽然是一句SQL,但效率很差。
- SQL code
insert into tb(Bh) select top 1 '001' from tb where not exists (select 1 from tb where bh='001')
------解决方案--------------------
我还就没想出来
------解决方案--------------------
insert into tb(Bh) select distinct '001' from tb where Bh<>'001'