--創(chuàng)建、刪除臨時(shí)表
--第一種方式
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
--查詢臨時(shí)表
select * from #tmp
--刪除臨時(shí)表
if object_id('tempdb..#tmp') is not null
begin
drop table #tmp
end
SQL Server臨時(shí)表的正確刪除方式
刪除SQL Server臨時(shí)表和一般表并不相同,下面將為您為別示例錯(cuò)誤和正確的刪除操作,供您參考,希望對您能夠有所幫助。
臨時(shí)表與一般的表不同,它是保存到tempDb表中。臨時(shí)表的表名與你所建的表名也不一樣,因?yàn)樗獮椴煌说南嗤僮鲃?chuàng)建不同的臨時(shí)表。
1、錯(cuò)誤的刪除操作:
--錯(cuò)誤的臨時(shí)表刪除操作,因?yàn)樗跀?shù)據(jù)庫不同
IF EXISTS (SELECT * FROM sysobjects WHERE object_id = OBJECT_ID(N'[dbo].[#tempTable]') AND type in (N'U'))
Begin
DROP TABLE [dbo].[tempTable]
End
--錯(cuò)誤的臨時(shí)表刪除操作,因?yàn)榕R時(shí)表名已變
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[#temptable]'))
Begin
drop table #temptable
End
2、正確的刪除方式:
--正確的臨時(shí)表刪除操作
if object_id('tempdb..#tempTable') is not null Begin
drop table #tempTable
End
sql 判斷臨時(shí)表是否存在,刪除臨時(shí)表重建
IF Object_id('Tempdb..#dl') IS NOT NULL
DROP TABLE #dl --如果有存在就刪除臨時(shí)表
CREATE TABLE #dl (neirong char(20),icount int, dlzonjine int, dlshu int, dlyin int) --重建臨時(shí)表
INSERT INTO #dl SELECT * FROM tab1 --把物理表的數(shù)據(jù)插到臨時(shí)表
您可能感興趣的文章:- SQLServer中臨時(shí)表與表變量的區(qū)別分析
- sqlserver 臨時(shí)表的用法
- sql server 臨時(shí)表 查找并刪除的實(shí)現(xiàn)代碼
- sql server中判斷表或臨時(shí)表是否存在的方法
- sqlserver 臨時(shí)表 Vs 表變量 詳細(xì)介紹
- SQL Server 向臨時(shí)表插入數(shù)據(jù)示例
- sqlserver 動態(tài)創(chuàng)建臨時(shí)表的語句分享
- SQL Server 表變量和臨時(shí)表的區(qū)別(詳細(xì)補(bǔ)充篇)
- sql server 創(chuàng)建臨時(shí)表的使用說明
- SQL SERVER臨時(shí)表排序問題的解決方法