首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-20 
小弟求一个SQL语句,如何知道某个记录在表中已经存在?我知道的有:delect@aasintegerifexists(select*fromta

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

热点排行
Bad Request.