SQL Server determine whether a table or database exists in the
SQL Server determine if the database exists:
Law (i):
SELECT * from master.dbo.sysdatabases where name= ' database name '
Law (ii):
If DB_ID (' database name ') is Notnull
Drop Database ...
Go
Create ...
SQL Server To determine whether the Table object exists:
Select COUNT (*) from sysobjects Whereid = object_id (' Database name '. Owner. Table name ')
if exists
(SELECT COUNT (*) from sysobjects where id = object_id (' database name. Owner. Table name '))
print ' exists '
Else
print ' does not exist '
SQL Server determine whether a field exists in the table:
if exists
(SELECT * from syscolumns where name= ' colname1 ' and id=object_id (' database name. Owner. Table name '))
print ' exists '
Else
print ' does not exist '
(Represents the Colname1 field in table TableName1)
Cases:
SELECT * from syscolumns wherename= ' Test ' and id=object_id (' Dbo.test ')
SQL Server To determine whether a stored procedure or view exists:
If object_id (' View or stored procedure name ') is not null
Drop Proc/view ...
Go
Create Proc/view ...
Or
If Exists (select * from sysobjects where name= ' view or stored procedure name ' and type = ' p/v ')
Drop Proc/view ...
Go
Create Proc/view ...
Determine whether a table or database exists in SQL Server