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 判斷臨時表是否存在,刪除臨時表重建

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  --把物理表的數據插到臨時表

 

--正確的臨時表刪除操作
if object_id('tempdb..#tempTable') is not null Begin
    drop table #tempTable
End


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM