SQL SERVER determines whether a database, table, view, trigger, stored procedure, function is present and deleted

Source: Internet
Author: User

--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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.