SQL Server two ways to create temporary tables and delete temporary tables _mssql

Source: Internet
Author: User
Tags numeric table name

--Create, delete temporary tables

--The first way 
create TABLE #tmp (name varchar (255), id int)-

-The second way
select COUNT (id) as Storynum,
sum (convert (Numeric (10,2), Case if IsNumeric (code) =1 then code else 0 end)) As Codenum,
sum ((Case when IsNumeric (realcode) =1 then convert (numeric (10,2), realcode) else 0.0)) as Realcodenum,
tdtname,cycle,jiracomponent,jirastatename,qualityvalue,storycodellt into 
#tmp from Iknow_story_ U2000V1R7C00 GROUP BY Tdtname,cycle,jiracomponent,jirastatename,qualityvalue,storycodellt

-Query temp table
SELECT * From #tmp-

-Deletes temporary table
if object_id (' tempdb ... #tmp ') is not null
	begin
		DROP table #tmp 
	

Correct deletion of SQL Server temp table

Deleting a SQL Server temporary table is not the same as a generic table, and the following will be a sample error for you and the correct deletion for your reference, in the hope of helping you.

A temporary table differs from a regular table in that it is saved to the tempdb table. The table name of the temporary table is not the same as the name of the table you built, because he wants to create a different temporary table for the same action for different people.

1, the wrong delete operation:

--incorrect temporary table deletion because the database is different
IF EXISTS (SELECT * from sysobjects where object_id = object_id (N ' [dbo].[ #tempTable] ') and type in (N ' U '))
 Begin
 DROP TABLE [dbo].[ TempTable]
End
 -the wrong temporary table deletion operation because the temporary table name has been changed
if exists (SELECT * from tempdb.dbo.sysobjects where id = object_id (N ' [#temptable])
Begin
 drop table #temptable End

2, the correct way to delete:

--The correct temporary table deletion operation
if object_id (' tempdb ... #tempTable ') is isn't null Begin
 drop table #tempTable
end

SQL to determine if temporary table exists, delete temporary table rebuild

IF object_id (' Tempdb.. #dl ') is a not NULL  
DROP table #dl--Deletes the temporary table
CREATE table #dl (Neirong char (), icount int, Dlzonjine int, DLS Hu int, Dlyin int)--Reconstructing temporary table
INSERT into #dl SELECT * tab1-inserting data from physical tables into temporary tables
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.