sql Server中暫存資料表與資料表的區別

來源:互聯網
上載者:User

標籤:刪除   dhx   into   wce   vpn   htm   from   jna   mba   

sql server 中暫存資料表與資料表的區別

1、如何判斷暫存資料表和資料表已產生

--如何判斷暫存資料表是否已建立---if exists(select * from tempdb..sysobjects where id=object_id(‘tempdb..#Temp_Student‘))    begin        print ‘存在暫存資料表‘;    endelse    begin        print ‘不存在暫存資料表‘;    end--如何判斷資料表是否已建立---if exists(select * from sys.tables where name=‘Data_Student‘)    begin        print ‘存在資料表‘;    endelse    begin        print ‘不存在資料表‘;    end

其中,暫存資料表建立後預設在tempdb(臨時資料庫中)的sysobjects中,而資料表建立在當前資料庫的sys.tables中

2、如何建立暫存資料表和資料表

  當我們檢查到暫存資料表、資料表不存在時,一般進行建立暫存資料表、資料表;存在時,可進行刪除或清空資料

--1、如何判斷暫存資料表是否已建立---if exists(select * from tempdb..sysobjects where id=object_id(‘tempdb..#Temp_Student‘))    begin        --print ‘存在暫存資料表‘;        --刪除暫存資料表(包括表結構)--        --drop table #Temp_Student                --刪除暫存資料表(不包括表結構)--        truncate table #Temp_Student    endelse    begin        --print ‘不存在暫存資料表‘;        create table #Temp_Student        (            Uid int identity(1,1) primary key,            Age int not null,            Name varchar(20) not null,        )    end    --2、如何判斷資料表是否已建立---if exists(select * from sys.tables where name=‘Data_Student‘)    begin        --print ‘存在資料表‘;        --刪除資料表(包括表結構)--        --drop table Data_Student                --刪除資料表(不包括表結構)--        truncate table Data_Student    endelse    begin        --print ‘不存在資料表‘;        --不存在時,建立資料表--        create table Data_Student        (            Uid int identity(1,1) primary key,            Age int not null,            Name varchar(20) not null,        )    end

3、如何添加暫存資料表和資料表的資料

-----3、如何插入資料------1)插入資料到暫存資料表---insert into #Temp_Student(Age,Name) values(21,‘張三‘),(22,‘李四‘) --2)插入資料到資料表---insert into Data_Student(Age,Name) values(23,‘王五‘),(24,‘趙六‘)

4、如何查詢暫存資料表和資料表資料

-----4、如何查詢資料-------    --1)查詢暫存資料表--select * from #Temp_Student--2)查詢暫存資料表--select * from Data_Student

5、查詢後結果

sql Server中暫存資料表與資料表的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.