Determine whether a table exists in SQL Server

Source: Internet
Author: User

In SQL Server (which should be said to be in all database products), create a resource such as a table, a view, and a stored procedure to determine whether the resource being created already exists   In SQL Server, you can generally find the results by querying the sys.objects system tables, but there are more convenient ways to  :     if   object_id (' tb_table ') are not Null          print ' exist '      else         print ' not ex Ist '   As above, can use OBJECT_ID () to quickly achieve the same purpose, Tb_table is the name of the resource I will create, so I want to first determine that the current database does not exist the same resource  object_id () can accept two parameters, The first one, as shown above, represents the name of the resource, which is the name of the table, but often we want to explain what type of resource we are creating,  so that SQL can explicitly look for duplicate names in one type of resource, as follows:     IF   object_id (' tb_table ', ' u ') is not null          print ' exist '      else          print ' not exist '   the second parameter "U" means that tb_table is a user-created table, that is: user_table initials abbreviated   Query sys.objects can get the type name of various resources (type column), here are a few main examples  u  -----------  user-created tables, different from system tables (user_table)  s  -----------  system table (system_table)  v  -----------  View  p  -----------  Stored Procedures (sql_stored_procedure)  You can use SELECT distinct type, type_desc from sys.objects to get all information   whether the library exists  if exists (SELECT * FROM master: sysdatabases where name=n ' library name ')  print ' exists ' elseprint ' not exists '--------------- --to determine if the name of the table to be created exists  if Exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Table name] ') and OBJECTPROPERTY (ID, N ' isusertable ') = 1)  --Delete table  drop tables [dbo]. [Table name] go --------------- -----column exists  if col_length (' table name ', ' column name ') is Nullprint ' not exists ' elseprint ' Exists ' ALTER TABLE name drop constraint default value name  go alter table name drop column name  go ----- -- Determine if the temporary table to be created exists  if object_id (' tempdb.dbo. #Test ') is not nullbeginprint ' exists ' endelsebeginprint ' does not exist ' End--------------- --Determine if the stored procedure name to be created exists  if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Stored procedure name] ') and OBJECTPROPERTY (ID, N ' isprocedure ') = 1)  --Delete stored procedure  drop procedure [dbo]. [Stored procedure name] go --------------- --to determine if the view name to be created exists  if exists (SELECT * From dbo.sysobjects WHERE id = object_id (N ' [dbo].[ View name] and OBJECTPROPERTY (ID, N ' isview ') = 1)  --Delete view  drop view [dbo]. [View name] go --------------- --to determine if the function name to be created exists  if exists (SELECT * from sysobjects where xtype= ' fn ' and Name= ' function name ')  if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Function name] ') and xtype in (n ' FN ', n ' IF ', n ' TF ')  --delete function  drop functions [dbo]. [Function name] go if col_length (' table name ', ' column name ') is Nullprint ' does not exist ' select 1 from sysobjects where ID in (SELECT ID from Syscol Umns where name= ' column name ') and name= ' table name '

SQL Server determines whether a table exists

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.