sql中exists的用法有哪些?

如题所述

exists的用法如下:

1、判断数据库是否存在

if exists (select*fromsysdatabaseswherename= '数据库名')

dropdatabase[数据库名]

2、判断表是否存在

if not exists (select * from sysobjects where [name] = '表名' and xtype='U')

begin

--这里创建表

end

3、判断存储过程是否存在

if exists (select*fromsysobjectswhereid = object_id(N'[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

dropprocedure[存储过程名]

4、判断视图是否存在

(1)SQL Server 2000

IF EXISTS (SELECT*FROMsysviewsWHEREobject_id = '[dbo].[视图名]'

(2)SQL Server 2005

IF EXISTS (SELECT*FROMsys.viewsWHEREobject_id = '[dbo].[视图名]'

5、判断函数是否存在

if exists (select*fromdbo.sysobjectswhereid = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))

dropfunction[dbo].[函数名]

扩展资料

SQL的提升

1、复制表(只复制结构,源表名:a 新表名:b) (Access可用)

法一:select * into b from a where 1<>1

法二:select top 0 * into b from a

2、拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)

insert into b(x, y, z) select d,e,f from a;

3、跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)

insert into b(x, y, z) select d,e,f from a in ‘具体数据库’ where 条件

例子:。.from b in '"&Server.MapPath("."&"\data.mdb" &"' where..

4、子查询(表名1:a 表名2:b)

select a,b,c from a where a IN (select d from b 或者: select a,b,c from a where a IN (1,2,3)

5、显示文章最后时间

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

参考资料:百度百科——SQL语句大全

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网