当前位置: 代码迷 >> Sql Server >> 小弟SQL语句,怎么知道某个记录在表中已经存在
  详细解决方案

小弟SQL语句,怎么知道某个记录在表中已经存在

热度:8   发布时间:2016-04-27 20:21:02.0
小弟求一个SQL语句,如何知道某个记录在表中已经存在?
我知道的有:
delect   @a   as   integer
if   exists(select   *   from   table1   where   Keyword= '01 ')
        @a=1
else
        @=0
方法2:
delect   @a   as   integer
select   @a=count(*)   from   table1   where   Keyword= '01 '
if   @a> 0
      记录存在
else
      记录不存在
我不知道哪个方法好,对服务器来说负担轻点,不知道其他大牛有没有什么更好的办法。谢谢指点

------解决方案--------------------
if exists(select top 1 1 from table1 where Keyword= '01 ')
------解决方案--------------------
应该第一个更有机会使用资源少,因为IF EXISTS是一遇到满足条件的就马上退出,不管后面的
而后面的SELECT 如果用上了索引还好些,如果没有索引,则慢很多.
------解决方案--------------------
if exists(select 1 from table1 where Keyword= '01 ')
print 1
else
print 0
  相关解决方案