Determine whether the ms SQL Server temporary table exists

Source: Internet
Author: User

Original article: http://www.cnblogs.com/kwklover/archive/2007/11/23/969633.html

 

Problem summary: Check whether the ms SQL Server temporary table exists

Drop table # tempcitys
Select * into # tempcitys from hy_citys

When the preceding statement is run for the first time, it must have encountered an error, but it will not be used for the second time.
Because select * into # tempcitys from hy_citys automatically creates a temporary table # tempcitys, the first temporary table does not exist, and a drop table error occurs naturally.
At the beginning, no response was made. Select * into will automatically create a temporary table.

Therefore, it is more reliable to determine whether a temporary table exists and then drop the table
If exists (select * From tempdb. DBO. sysobjects where id = object_id (N 'tempdb .. # tempcitys ') and type = 'U ')
Drop table # tempcitys

Note that tempdb is followed by two. Not one

Consider writing another SQL statement
Insert into # tempcitys (cityid) Select cityid from hy_citys
In this way, # tempcitys will not be automatically created. To use # tempcitys, you must first create table # tempcitys (cityid INT)

I am confused. The result of poor learning skills. :(

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.