SQL 语句判断记录是否存在

发布时间:2017-09-21编辑:佚名阅读(2020)

select isnull((select top(1) 1 from tableName where conditions), 0)

网上的常见的有问题的语句:

错误1:

select COUNT(*) from tableName where conditions

count(*) 计算会对聚集索引进行扫描和计算,reads 特别多,特别是 cpu 资源占用极大。

错误2:

if exists (select * from tableName where conditions) select '1' else select '0'

这种方法稍微有点灵性,但是却很不简洁。

同时由于 select * 前面有 exists ,所以该语句并没有性能问题,只是语法不够简洁


  关键字:SQL判断记录是否存在


鼓掌

0

正能量

0

1

呵呵

1


评论区