sqlserver 建表、建索引,建存储过程、增加列

--创建表
--创建表之前判断表是否存在
if OBJECT_ID('testtable',N'U') is  null
begin
create table testtable
(id int,
 name varchar(20),
 phone varchar(21)
 )
 end

--创建索引
--创建索引之前判断索引是否存在
if exists ( SELECT 1 FROM sys.indexes WHERE object_id=OBJECT_ID('testtable', N'U') and NAME='testtable_id')
begin
 drop index testtable.testtable_id
 print '删除索引'
 end
 else
 begin
 create  unique clustered index testtable_id on testtable(id)
 print '创建索引'
 end
 
 
 --创建存储过程 
 --创建存储过程之前,先判断存储过程是否存在
USE [MYDB]
GO
if exists (select 1 from sysobjects where id=OBJECT_ID(N'procedurename') and OBJECTPROPERTY(id,N'IsProcedure')=1 ) 
drop procedure procedurename
print'删除存储过程'
/****** Object:  StoredProcedure [dbo].[procedurename]    Script Date: 01/26/2014 10:02:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create PROCEDURE  [dbo].[procedurename] 
as
select name from testtable
GO
print '创建存储过程'


--增加字段活动ID
USE MS40SendSongDB
GO
if not exists(select 1 from syscolumns where id=object_id('UserSendInfo') and name='ActivityId')
alter table UserSendInfo add ActivityId varchar(128) 
GO

猜你喜欢

转载自wt-kelly.iteye.com/blog/2009466