Check whether the database exists in SQL Server:
Method (1 ):
Select * from Master. DBO. sysdatabases where name = 'database name'
Method (2 ):
If db_id ('database name') is not null
Drop database...
Go
Create...
Check whether the table object exists in SQL Server:
Select count (*) from sysobjects where id = object_id ('database name. Owner. Table name ')
If exists
(Select count (*) from sysobjects where id = object_id ('database name. Owner. Table name '))
Print 'exist'
Else
Print 'nonexistent'
In SQL Server, check whether the fields in the table exist:
If exists
(Select * From syscolumns where name = 'colname1' and ID = object_id ('database name. Owner. Table name '))
Print 'exist'
Else
Print 'nonexistent'
(Indicates that the table tablename1 contains the colname1 field)
Example:
Select * From syscolumns where name = 'test' and ID = object_id ('dbo. test ')
Check whether a stored procedure or view exists in SQL Server:
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...