sql server創(chuàng)建臨時表的兩種寫法和刪除臨時表
--創(chuàng)建、刪除臨時表
--第一種方式 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表中。臨時表的表名與你所建的表名也不一樣,因為他要為不同人的相同操作創(chuàng)建不同的臨時表。
1、錯誤的刪除操作:
--錯誤的臨時表刪除操作,因為所在數(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 --錯誤的臨時表刪除操作,因為臨時表名已變 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 --把物理表的數(shù)據(jù)插到臨時表
相關(guān)文章
sql server通過pivot對數(shù)據(jù)進(jìn)行行列轉(zhuǎn)換的方法
這篇文章主要介紹了sql server通過pivot對數(shù)據(jù)進(jìn)行行列轉(zhuǎn)換的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05SQLServer中bigint轉(zhuǎn)int帶符號時報錯問題解決方法
用一個函數(shù)來解決SQLServer中bigint轉(zhuǎn)int帶符號時報錯問題,經(jīng)測試可用,有類似問題的朋友可以參考下2014-09-09SQL Server簡單模式下誤刪除堆表記錄恢復(fù)方法(繞過頁眉校驗)
這篇主旨是揭示堆表的刪除記錄找回的原理,我所考慮的方面并不適用于每個人的每種情況,望大家見諒2013-01-01SQL Server 2012 身份驗證(Authentication)
這篇SQL Server安全文章,我們學(xué)習(xí)了SQL Server里的多個驗證選項。Windows集成身份驗證是最安全的,但并不是都是可行的,微軟多年來已經(jīng)讓SQL Server驗證更加安全。2016-04-04sql存儲過程實例--動態(tài)根據(jù)表數(shù)據(jù)復(fù)制一個表的數(shù)據(jù)到另一個表
這篇文章主要介紹了sql存儲過程實例--動態(tài)根據(jù)表數(shù)據(jù)復(fù)制一個表的數(shù)據(jù)到另一個表的相關(guān)資料,需要的朋友可以參考下2017-10-10