sql server创建临时表的两种写法和删除临时表

栏目: 数据库 · SQL Server · 发布时间: 8年前

内容简介:这篇文章主要介绍了sql server创建临时表的两种写法和删除临时表 ,需要的朋友可以参考下

--创建、删除临时表

--第一种方式 
create table #tmp(name varchar(255),id int)

--第二种方式
select count(id) as storyNum ,
sum(convert(numeric(10,2),case when 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 end)) as realcodeNum,
tdtname,cycle,jiracomponent,jirastatename,qualityvalue,storycodellt 
into #tmp from IKNOW_STORY_U2000V1R7C00 group by tdtname,cycle,jiracomponent,jirastatename,qualityvalue,storycodellt

--查询临时表
select * from #tmp

--删除临时表
if object_id('tempdb..#tmp') is not null
	begin
		drop table #tmp 
	end 

SQL Server临时表的正确删除方式

删除SQL Server临时表和一般表并不相同,下面将为您为别示例错误和正确的删除操作,供您参考,希望对您能够有所帮助。

临时表与一般的表不同,它是保存到tempDb表中。临时表的表名与你所建的表名也不一样,因为他要为不同人的相同操作创建不同的临时表。

1、错误的删除操作:

--错误的临时表删除操作,因为所在数据库不同
IF EXISTS (SELECT * FROM sysobjects WHERE object_id = OBJECT_ID(N'[dbo].[#tempTable]') AND type in (N'U'))
 Begin
 DROP TABLE [dbo].[tempTable]
End
 --错误的临时表删除操作,因为临时表名已变
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[#temptable]'))
Begin
 drop table #temptable
End

2、正确的删除方式:

--正确的临时表删除操作
if object_id('tempdb..#tempTable') is not null Begin
 drop table #tempTable
End

sql 判断临时表是否存在,删除临时表重建

IF Object_id('Tempdb..#dl') IS NOT NULL  
DROP TABLE #dl --如果有存在就删除临时表
CREATE TABLE #dl (neirong char(20),icount int, dlzonjine int, dlshu int, dlyin int) --重建临时表
INSERT INTO #dl SELECT * FROM tab1 --把物理表的数据插到临时表

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to Genetic Algorithms

An Introduction to Genetic Algorithms

Melanie Mitchell / MIT Press / 1998-2-6 / USD 45.00

Genetic algorithms have been used in science and engineering as adaptive algorithms for solving practical problems and as computational models of natural evolutionary systems. This brief, accessible i......一起来看看 《An Introduction to Genetic Algorithms》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具