SQL中EXISTS怎么用 ?

SQL中EXISTS怎么用 


牛魔王的故事
浏览 493回答 2
2回答

泛舟湖上清波郎朗

exists的用法如下:1、判断数据库是否存在if exists (select*fromsysdatabaseswherename= '数据库名')dropdatabase[数据库名]2、判断表是否存在if not exists (select * from sysobjects where [name] = '表名' and xtype='U')begin--这里创建表end3、判断存储过程是否存在if exists (select*fromsysobjectswhereid = object_id(N'[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)dropprocedure[存储过程名]4、判断视图是否存在(1)SQL Server 2000IF EXISTS (SELECT*FROMsysviewsWHEREobject_id = '[dbo].[视图名]'(2)SQL Server 2005IF 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 a2、拷贝表(拷贝数据,源表名: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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

SQL Server