日期:2014-05-19  浏览次数:20528 次

小弟求一个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