SQL existence judgment

Source: Internet
Author: User

Delete if Yes


-- Determines whether the specified database exists. If yes, the database is deleted.

If exists (select name from master .. sysdatabases where name in (''db _ name ''))

Drop database db_name

Go

-- Determines whether the specified stored procedure exists and deletes it if it exists.

If exists (select * from sysobjects where objectproperty (object_id (''proc _ name''), ''isprocedure '') = 1)

Drop procedure proc_name

Go

-- Determines whether the specified table exists. If yes, the table is deleted.

If exists (select * from sysobjects where objectproperty (object_id (''table _ name''), ''istable'') = 1)

Drop table table_name

Go

-- Determines whether the specified custom function exists. If yes, delete it.

If exists (select * from sysobjects where objectproperty (object_id (''dbo. func_name ''), ''isansinullson'') = 1)

Drop function dbo. func_name

Go

-- Determines whether the specified temporary table exists and deletes it if it exists.

If exists (select * from tempdb .. sysobjects where name like ''# table_name % '')

Drop table # table_name

Go

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.