Whether the temporary table exists

Source: Internet
Author: User

1. in SQL Server, use select object_id ('tempdb .. # temptable') to determine whether a temporary table has been created. (The returned value is an integer. If no value is set, the return value is null)

Tempdb is a database that stores temporary tables.

Before creating a temporary table, use the following statement to determine whether the temporary table already exists. If the temporary table already exists, delete it.

If not (select object_id ('tempdb .. # temptable') is null drop table # temptable

Eg: If not (select object_id ('tempdb .. #1234 ') is null drop table #1234 easier writing three functions are the same

If exists (select * From tempdb. DBO. sysobjects where id = object_id (N 'tempdb .. #1234 ') Drop table #1234

If exists (select * From tempdb .. sysobjects where id = object_id (N 'tempdb .. #1234 ') execute ('drop table #1234 ')

2. Temporary tables are similar to permanent tables, but temporary tables are stored in tempdb. They are automatically deleted when they are no longer used. Temporary tables can be local or global. They differ in terms of name, visibility, and availability.

The name of the local temporary table starts with a single digit (#). They are only visible to the current user connection and are deleted when the user is disconnected from the SQL server instance.

The name of the global temporary table starts with two numeric symbols (#). After being created, the table is visible to all users. When all users that reference the table are disconnected from SQL Server, the table is deleted.

3. Permanent Table id = object_id (n' [DBO]. [dh_sjmt] ')

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [dh_sjmt] ') and objectproperty (ID, N 'isusertable') = 1) Drop table [DBO]. [dh_sjmt]

4. Select * From DBO. sysobjects to query the Permanent Table name. Name/ID/xtype the xtype = 'U'

Select [name] From tempdb .. sysobjects query the temporary table name. Name/ID/xtype the xtype = 'U'

 

 

5. Case Analysis

 

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [tempkh] ') and objectproperty (ID, n'isusertable') = 1)

Drop table [DBO]. [tempkh]

Go

// Export the built-in footstep statements

Create Table [DBO]. [tempkh] (

[Stypeno] [varchar] (15) Collate chinese_prc_ci_as null,

[Colorid] [varchar] (10) Collate chinese_prc_ci_as null

) On [primary]

Go

// Check whether the function exists if the function is the same

If exists (select * From DBO. sysobjects where objectproperty (object_id (n' [DBO]. [tempkh] '), n' isusertable') = 1) -- 'istable') = 1)

Drop table [DBO]. [tempkh]

Go

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.