sql server 表變數、表類型、暫存資料表

來源:互聯網
上載者:User

標籤:

sql server 中暫存資料表分為會話暫存資料表和永久暫存資料表。會話暫存資料表在會話結束後自動被刪除,永久暫存資料表與基本表的使用上基本無差異,需要顯示調用drop將其刪除。

建立暫存資料表

建立會話暫存資料表

create table #table_name(column_name datatype constraint_name[.....]);

建立永久暫存資料表

create table ##table_name(column_name datatype constraint_name[.....]);

暫存資料表的約束可以在建立表之後建立,使用

alter table table_name add constraint contraint_name;語句建立。

表類型

表類型是一個使用者自訂類型,使用者可以建立自己所需要的表類型,說白了就是把表結構和約束預先建立好,後面要使用的時候直接根據該表類型建立表變數。

建立表類型

create type type_name as table(column_name datatype constraint_name[.....]);

在這裡表中的約束必須在建立類型的時候就建立,不能使用alter語句。

根據表類型申明表變數

declare @table_variable type_name;

表變數

表變數是一種資料類型,該類型具有表的結構和部分表的功能。可以對其進行查詢、插入、更新、刪除。值得注意的是表變數不能使用select .. into語句插入資料

但是暫存資料表可以。表變數可以結合預存程序、函數等程式塊使用。表變數與其他基本變數類型的方法和申明一致。

申明表變數

declare @table_variable table(column_name datatype constraint_name[.....]); 

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.