--SQL SERVER determines if there is a trigger, stored procedure
--Determine the storage process and delete if it exists
IF (EXISTS (SELECT * from sysobjects WHERE name= ' procedurename ' and type= ' P '))
DROP PROCEDURE procedurename
--Judgment trigger, if present delete
IF (EXISTS (SELECT * from sysobjects WHERE id=object_id (N ' [dbo].[ Triggername] ') and OBJECTPROPERTY (ID, N ' istrigger ') = 1))
DROP TRIGGER Triggername
--Determine if the user function exists and delete if it exists
--There are two types here: ' TF '-table-value function table-valued functions ' FN '-scalar-value function scalar-valued functions
IF (EXISTS (SELECT * from sysobjects WHERE id=object_id (N ' [dbo].[ Userfunction] and (type= ' FN ' OR type= ' TF ')))
DROP FUNCTION userfunction
--Determine whether the view exists, or delete
IF (EXISTS (SELECT table_name from INFORMATION_SCHEMA. Views WHERE table_name=n ' viewname '))
DROP VIEW viewname
--Determine if the user table exists and delete if it exists
IF (EXISTS (SELECT * from sysobjects WHERE id=n ' tablename ' and OBJECTPROPERTY (ID, N ' isusertable ') =1))
DROP TABLE TableName
--Judge the database if it exists and delete it
IF (EXISTS (SELECT * from master.dbo.sysdatabases WHERE dbid=db_id (' dbname ')))
DROP DATABASE dbname
--If you are prompted that the database is being used when you delete the database, you cannot delete it (cannot drop database databasename because it is currently on use), using:
IF (EXISTS (SELECT * from master.dbo.sysdatabases WHERE dbid=db_id (' dbname ')))
BEGIN
Use master
ALTER DATABASE dbname
SET Single_user
With ROLLBACK IMMEDIATE
DROP DATABASE dbname
END
SQL SERVER determines whether a database, table, view, trigger, stored procedure, function is present and deleted