How can I determine whether a temporary table exists before being created? SQL Server database

Source: Internet
Author: User

Create Table # report
(
Id int identity primary key,
Bookid int,
Bookname varchar (50)
)
Go
If exists (select * From tempdb .. sysobjects where id = object_id ('tempdb .. # Report '))
Select '# The report table exists! '
Else
Select '# The report table does not exist!

If exists (select * From tempdb .. sysobjects where id = object_id ('tempdb .. # Report '))
Begin
Select '# The report table exists! '
Drop table # report
End
Else
Select '# The report table does not exist! '
-----------------------
If exists (select top 1 * from # Report)
Begin
....
End
-----------------------------------------------
Method 1:
If object_id ('tempdb .. yourtemptablename') is not null
Drop table tempdb .. yourtemptablename

Method 2:
If exists (select 1 from tempdb .. sysobjects where type = 'U' and name like 'yourtemptablename % ')
Drop table tempdb .. yourtemptablename
Comparison
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [infocategory] ') and objectproperty (ID, n'isusertable') = 1)
Drop table [DBO]. [infocategory]

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.