The following objects are judged for existence: Database, table, proc, trigger, temp table, index.
1. Judge the database:
if exists (SELECT * from sys.databases WHERE name = ' database name ')
drop database [DB name]
2. Judgment table:
SQL code
if exists (select * from sysobjects where id = object_id (n ' [table name] ') and OBJECTPROPERTY (ID, n ' isusertable ') = 1)
drop table [table name]
3. Determine the stored procedure
if exists (select * from sysobjects where id = object_id (n ' [Stored procedure name] ') and OBJECTPROPERTY (ID, n ' isprocedure ') = 1)
drop procedure [Stored procedure name]
4. Judging the temporary table
If object_id (' tempdb.. #临时表名 ') is not null
drop table #临时表名
5. Judgment View
--sql Server 2000
IF EXISTS (SELECT * from sysviews WHERE object_id = ' [dbo].[ View name] '
--sql Server 2005
IF EXISTS (SELECT * from sys.views WHERE object_id = ' [dbo].[ View name] '
6. Judgment function
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Function name] ') and xtype in (n ' FN ', n ' IF ', n ' TF '))
Drop function [dbo]. [function name]
7. Get user-created object information
SELECT [name],[id],crdate from sysobjects where xtype= ' U '
/*
the xtype represents the parameter types, usually including the following
C = CHECK constraint
D = defaults or Default constraints
F = FOREIGN KEY constraint
L = Log
FN = Scalar function
IF = inline table function
P = stored procedure
PK = PRIMARY KEY constraint (type is K)
RF = copy Filter stored procedure
S = system table
TF = table function
TR = Trigger
U = User table
UQ = UNIQUE constraint (type is K)
V = View
X = extended stored Procedure
*/SELECT [name],[id],crdate from sysobjects where xtype= ' U '
/*
the xtype represents the parameter types, usually including the following
C = CHECK constraint
D = defaults or Default constraints
F = FOREIGN KEY constraint
L = Log
FN = Scalar function
IF = inline table function
P = stored procedure
PK = PRIMARY KEY constraint (type is K)
RF = copy Filter stored procedure
S = system table
TF = table function
TR = Trigger
U = User table
UQ = UNIQUE constraint (type is K)
V = View
X = extended stored Procedure
*/
8. Judging column
if exists (SELECT * from syscolumns where id=object_id (' table name ') and name= ' column name ')
ALTER TABLE table name drop column name
9. Determine if the column is self-increment
If ColumnProperty (object_id (' table '), ' col ', ' isidentity ') =1
print ' Self-increment column '
Else
print ' is not self-adding column '
SELECT * from Sys.columns WHERE object_id=object_id (' Table name ')
and Is_identity=1
10. Determine if an index exists in the table
if exists (SELECT * from sysindexes where id=object_id (' table name ') and name= ' index name ')
print ' exists '
Else
print ' does not exist
11. Viewing objects in the database
SELECT * from sys.sysobjects WHERE name= ' object name '
sqlserver--to determine whether an object exists