现在的位置: 首页 > 综合 > 正文

常用sql语句整理

2013年10月01日 ⁄ 综合 ⁄ 共 2566字 ⁄ 字号 评论关闭

1SQL判断数据库、表、存储过程、视图、函数、列是否存在

a、判断数据库是否存在

if exists(select * from sys.databases where name=’库名’)

--删除数据库

Drop database 库名

b、判断要创建的表名是否存在

if exists(select * from dbo.sysobjects where id=object_id(N'[dbo].[表名]')and

OBJECTPROPERTY(id, N'IsUserTable') = 1)

-- 删除表
drop table [dbo].[
表名]

c--判断要创建临时表是否存在

If  Object_Id('Tempdb.dbo.#临时表名') Is Not Null

drop table #临时表名

d、判断要创建的存储过程名是否存在

if exists(select * from dbo.sysobjects where id=object_id(N'[dbo].[存储过程名]')and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

-- 删除存储过程
drop procedure [dbo].[
存储过程名]

f 判断要创建的视图名是否存在

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[视图名]') and OBJECTPROPERTY(id, N'IsView') = 1)
--
删除视图

drop view [dbo].[
视图名]
g、判断要创建的函数名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))
--
删除函数

drop function [dbo].[
函数名]

h、判断列是否存在

if exists(select * from syscolumns where id=object_id('表名') and name='列名')

--删除列

alter table 表名 drop column 列名

i、判断表中是否存在索引

if exists(select * from sysindexes where id=object_id('表名') and name='索引名')

 

 

 

2、表结构的修改

a1、表的创建

create table tabname

(col1 type1 [not null] [primary key],

col2 type2 [not null],..)

a2:使用旧表创建新表

select * into 目的数据库名.dbo.目标表名 from 原表名

a3:使用旧表创建新表

create table 新表名 as select1,2… from 旧表名definition only

a4:复制表(只复制结构,源表名:a  新表名:b)

select * into b from a where 1<>1

 

b、增加列

IF NOT EXISTS

(

         SELECT 1 FROM syscolumns sc,sysobjects so

    WHERE sc.id=so.id AND sc.name='列名' AND so.name='表名'

)

BEGIN

 ALTER TABLE 表名 ADD 列名类型 NOT NULL DEFAULT('')

END

c、修改列

DECLARE @name VARCHAR(100)

 DECLARE @sql VARCHAR(1000)

 SELECT @name = B.name FROM syscolumns A,sysobjects B WHERE A.id=object_id('表名') AND A.name = '列名' AND B.id=A.cdefault AND B.name LIKE 'DF%'

 SET @sql = N'ALTER TABLE [表名] DROP CONSTRAINT ' + @name

 exec (@sql)

 Alter Table表名Alter Column列名 类型 NOT NULL

 SET @sql = N'ALTER TABLE [表名] ADD CONSTRAINT ' + @name + N' DEFAULT (''0'') FOR [列名]'

 exec (@sql)

 

3、表内数据的操作

a1:清空表内的数据

truncate table 表名

a2:删除表内的数据

DELETE FROM 表名 WHERE 列名称 =

a3:删除表内的数据

DELETE * FROM 表名

b、更新表内的数据

UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值

c1、向表格内插入数据

INSERT INTO 表名 VALUES (1, 2,....)

C2、向表格内插入数据

INSERT INTO 表名 (1, 2,...) VALUES (1, 2,....)

 

C3:将一张表内数据同步至另一张表

4、数据查找

a1:查找某一列数据

SELECT 列名称 FROM 表名称

a2:查找所有数据

SELECT * FROM 表名称

a3sql插入语句得到自动生成的递增ID

Insert into 表名1(1,列2)values(1,值2)

Select @@identity as ‘ID’

a4:嵌套子查询

select 1,列2 from 表名1 where 1 in(select 1 from 表名2)

a5:随机提取条记录

select top 10 * from 表名 order by NewID()

a6:得到无重复记录的结果集

select distinct * from 表名

a7:得到某两个值之间的数据

select * from 表名 where 列名1 between 1 and 2

a8:外连接查询(表名1a  表名2b)

select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

a9:四表联查

select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....   

 

 

 

通配符的一些用法

b1:选择列名1中首字母在A-M之间的记录

select * from 表名 where 列名1 like [A-M]%

b2: 选择列名1中首字母是AB、或C的记录

select * from 表名 where 列名1 like [ABC]%

b3: 选择列名1中首字母在A-C之间的或者是G的记录

抱歉!评论已关闭.