Determine whether a table or temporary table exists in SQL Server

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/yugen/archive/2010/07/25/1784749.html

1. Determine if the data table exists

Method One:

Use Yourdb;
Go

If OBJECT_ID (n ' tablename ', n ' U ') is not null
print ' exists '
Else
print ' does not exist '


For example:
Use Fireweb;
Go

If OBJECT_ID (n ' temp_tbl ', n ' U ') is not null
print ' exists '
Else
print ' does not exist '

Method Two:

Use [instance name]
GO

IF EXISTS (SELECT * FROM dbo. SysObjects WHERE id = object_id (N ' [table name] ') and OBJECTPROPERTY (ID, ' istable ') = 1)
PRINT ' exists '
ELSE
PRINT ' does not exist '


For example:
Use Fireweb;
Go

IF EXISTS (SELECT * FROM dbo. SysObjects WHERE id = object_id (N ' temp_tbl ') and OBJECTPROPERTY (ID, ' istable ') = 1)
PRINT ' exists '
ELSE
PRINT ' does not exist '

2. Whether the temporary table exists:

Method One:
Use Fireweb;
Go

if exists (SELECT * from tempdb. sysobjects where id=object_id (' tempdb. # #TEMP_TBL '))
PRINT ' exists '
ELSE
PRINT ' does not exist '


Method Two:
Use Fireweb;
Go

if exists (SELECT * from tempdb.dbo.sysobjects where id = object_id (N ' tempdb): #TEMP_TBL ') and type= ' U ')
PRINT ' exists '
ELSE
PRINT ' does not exist '

Determine whether a table or temporary table exists in SQL Server

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.