SQL Server determines whether a database, table, column, or view exists

Source: Internet
Author: User

Original http://www.cnblogs.com/zhenpingwang/archive/2013/04/15/3021979.html

1. Determine whether the database exists If exists (select * From SYS. databases where name = 'database name ') Drop database [database name]

2. Check whether the table exists. If exists (select * From sysobjects where id = object_id (n '[Table name]') and objectproperty (ID, N 'isusertable') = 1) Drop table [Table name]

3. Determine whether a stored procedure exists If exists (select * From sysobjects where id = object_id (n' [stored procedure name] ') and objectproperty (ID, n' isprocedure') = 1) Drop procedure [stored procedure name]

4. Determine whether a temporary table exists If object_id ('tempdb .. # temporary table name') is not null Drop table # temporary table name

5. Determine whether a view exists 

-- Determine whether 'myview52' exists. If exists (select table_name from information_schema.views where table_name = n 'myview52') print ': 'else print' does not exist'6. Determine whether a function exists.-- Determine whether the name of the function to be created existsIf exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [function name] ') and xtype in (n'fn', n'if', n'tf ')) Drop function [DBO]. [function name]

7. obtain information about the object created by the user

Select [name], [ID], crdate from sysobjects where xtype = 'U'

/* Xtype indicates the parameter type, the following c = check constraints are usually included: D = default value or default constraint F = foreign key constraint L = Log fn = scalar function if = embedded table function P = Stored Procedure PK = primary key constraint (K type) rf = copy and filter Stored Procedure S = system table TF = table function TR = trigger u = User table uq = unique constraint (type: K) V = view X = Extended Stored Procedure */

8. Determine whether a Column exists If exists (select * From syscolumns where id = object_id ('table name') and name = 'column name ') Alter table Table Name drop column name

9. Determine whether the column is an auto-incrementing column. If columnproperty (object_id ('table'), 'col', 'isidentity ') = 1 Print 'auto-incrementing column' Else Print 'not auto-incrementing column' Select * From SYS. columns where object_id = object_id ('table name') and is_identity = 1

10. Check whether an index exists in the table.

If exists (select * From sysindexes where id = object_id ('table name') and name = 'index name ') Print 'exist' Else Print 'nonexistent'

11. view objects in the database

Select * From SYS. sysobjects where name = 'object name' select * From SYS. sysobjects where name = 'object name'

1. Determine whether the database exists If exists (select * From SYS. databases where name = 'database name ') Drop database [database name]

2. Check whether the table exists. If exists (select * From sysobjects where id = object_id (n '[Table name]') and objectproperty (ID, N 'isusertable') = 1) Drop table [Table name]

3. Determine whether a stored procedure exists If exists (select * From sysobjects where id = object_id (n' [stored procedure name] ') and objectproperty (ID, n' isprocedure') = 1) Drop procedure [stored procedure name]

4. Determine whether a temporary table exists If object_id ('tempdb .. # temporary table name') is not null Drop table # temporary table name

5. Determine whether a view exists 

-- Determine whether 'myview52' exists. If exists (select table_name from information_schema.views where table_name = n 'myview52') print ': 'else print' does not exist'6. Determine whether a function exists.-- Determine whether the name of the function to be created existsIf exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [function name] ') and xtype in (n'fn', n'if', n'tf ')) Drop function [DBO]. [function name]

7. obtain information about the object created by the user

Select [name], [ID], crdate from sysobjects where xtype = 'U'

/* Xtype indicates the parameter type, the following c = check constraints are usually included: D = default value or default constraint F = foreign key constraint L = Log fn = scalar function if = embedded table function P = Stored Procedure PK = primary key constraint (K type) rf = copy and filter Stored Procedure S = system table TF = table function TR = trigger u = User table uq = unique constraint (type: K) V = view X = Extended Stored Procedure */

8. Determine whether a Column exists If exists (select * From syscolumns where id = object_id ('table name') and name = 'column name ') Alter table Table Name drop column name

9. Determine whether the column is an auto-incrementing column. If columnproperty (object_id ('table'), 'col', 'isidentity ') = 1 Print 'auto-incrementing column' Else Print 'not auto-incrementing column' Select * From SYS. columns where object_id = object_id ('table name') and is_identity = 1

10. Check whether an index exists in the table.

If exists (select * From sysindexes where id = object_id ('table name') and name = 'index name ') Print 'exist' Else Print 'nonexistent'

11. view objects in the database

Select * From SYS. sysobjects where name = 'object name' select * From SYS. sysobjects where name = 'object name'

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.