SQL中臨時表的建立方法

2021-04-21 05:55:51 字數 2045 閱讀 4401

使用臨時表,可以減少sql文的執行次數,提高程式的執行效率。

1. 物理臨時表

與資料表一樣,在資料庫建立的時候建立,長期存在資料庫中。程式在執行的時候,臨時將資料放入其中,在檢查或者修改完成之後,將其中的資料轉移到其他資料表中。程式結束之後,刪除其中的資料。

2. 連線式臨時表

與資料庫建立連線的同時建立該臨時表,之後的作用與【物理臨時表】一致。在連線斷開之後,資料庫系統刪除該臨時表。

3. 笛卡爾乘積式臨時表

如果操作的資料是兩個表中的資料的笛卡爾乘積,可以在sql中通過兩個表的內聯,建立這樣的臨時表資料。

select * from

( select

店鋪code, 商品code

from t_

店鋪表, t_商品表

where

店鋪code in ('店鋪code1', '店鋪code2')

and

商品code in ('商品code1', '商品code2')

) as t_

店鋪商品

left join t_

其他表on t_

其他表.店鋪code = t_店鋪商品.店鋪code

and t_

其他表.商品code = t_店鋪商品.商品code

4. 內聯式臨時表

在sql中將資料用查詢固定值的方式內聯形成臨時表,然後用該臨時表聯結其他資料表。

select t_

企劃_商品

.商品code

, t_

企劃_商品

.企畫番號

, t_

企劃_商品

.企畫開始日

, t_

企劃_商品

.企畫終了日

from

(select '00000000000001' as

商品code

, '20080515' as

訂貨開始日

, '20080519' as

訂貨結束日

union all

select '00000000000002' as

商品code

, '20080516' as

訂貨開始日

, '20080520' as

訂貨結束日

union all

select '00000000000003' as

商品code

, '20080515' as

訂貨開始日

, '20080521' as

訂貨結束日

union all

select '00000000000004' as

商品code

, '20080514' as

訂貨開始日

, '20080522' as

訂貨結束日

union all

select '00000000000005' as

商品code

, '20080513' as

訂貨開始日

, '20080523' as

訂貨結束日

union all

select '00000000000006' as

商品code

, '20080512' as

訂貨開始日

, '20080524' as

訂貨結束日

) as checkdata

inner join t_

企劃_商品

on t_

企劃_商品

.商品code

= checkdata.

商品code

and t_

企劃_商品

.訂貨開始日

<= checkdata.

訂貨結束日

and t_

企劃_商品

.訂貨結束日

>= checkdata.

訂貨開始日

order by m_企畫_

商品.商品code

oracle中建立臨時表方法

1.首先必須有許可權 2.建表的語法示例 建立虛擬表 create global temporary table test ssid int,sname varchar2 20 on commit delete rows 插入資料 insert into test values 1,200 查詢資料...

oracle中建立臨時表方法

1.首先必須有許可權 按照上面兩位說得一定可以。2.建表的語法示例 建立虛擬表 create global temporary table test ssid int,sname varchar2 20 on commit delete rows 插入資料 insert into test valu...

SQL語句建立臨時表

1.insert into select語句 語句形式為 insert into table2 field1,field2,select value1,value2,from table1 要求目標表table2必須存在,由於目標表table2已經存在,所以我們除了插入源表table1的字段外,還可...