在sql语名中,if not exists 即如果不存在,if exists 即如果存在。
下面学习下二者的用法。
a,判断数据库不存在时
代码如下:if not exists(select * from sys.databases where name = ‘database_name’)
b,判断表不存在时
代码如下:if not exists (select * from sysobjects where id = object_id(‘table_name’) and OBJECTP
一、关于exists查询
explain select * from vendor where EXISTS(select * from area where area_code = vendor_prov_code ) limit 10
以上是一个典型的exists查询的sql语句。
它的作用方式是这样的:每次从vendor表中查询出一条数据,然后将这条数据中的vendor_prov_code值传递到exists查询中进行执行,也就是进行子查询的执行。
如果子查询查到的数据就返回布尔值tru