一、視圖
1)創建視圖
create view [view_name]
as
[select express]
2)示例:
create view good_stu
as
select stu.* from student s,grade g where s.id=g.sid and g.score>80
3)執行
select * from [view_name]
二、臨時表
特點:臨時表存儲在tempdb中,當不再使用時會自動刪除
1)本地臨時表
create table #[table_name]
(columns...)
--插入數據
insert into #[table] values (...)
insert into #tt select id,name from student;
可見性:只在當前數據庫連接可見;
2)全局臨時表
create table ##[table_name]
(columns...)
可見性:全局可見
三、表變量
使用場景:在一個存儲過程中存儲多段結果集
1)聲明
declare @tmp table --聲明表變量
(
id int identity(1,1), --字段 必須和插入表變量裏的數量一一對應
Name varchar(60),
[Description] varchar(60),
Category varchar(60)
)
2)插入數據
insert @tmp select col1,col2,col2.... from [table_name] where [express]
在select投影的字段名稱和順序必須與表變量中的一致
四、比較