符合条件的记录有则修改没有则添加的小优化技巧

通常的写法:

if(select count(1) from table where id=XXX)=0

  insert into XXX

else

  update table set xxx where id=XXX

优化后的写法:

update table set xxx where id=XXX

if @@rowcount=0 

  insert into XXX

第一种情况,无论如何都会执行两次操作,第二种情况只会运行一次操作!!

原文地址:https://www.cnblogs.com/davidhou/p/5184167.html