1. 程式人生 > >T-SQL 視圖

T-SQL 視圖

ssi exist from sharp core ntc res where --

 1 use StudentManager
 2 go
 3 --判斷視圖是否存在
 4 if exists(select * from sysobjects where name=view_ScoreQuery)
 5 drop view view_ScoreQuery
 6 go
 7 --創建視圖
 8 create view view_ScoreQuery
 9 as
10     select top 1000 Students.StudentId,StudentName,ClassName,
11              C#=CSharp,SQLDB=SQLServerDB,
12
ScoreSum=(CSharp+SQLServerDB) from Students 13 inner join ScoreList on Students.StudentId=ScoreList.StudentId 14 inner join StudentClass on Students.ClassId=StudentClass.ClassId 15 order by StudentClass.ClassId 16 go 17 --使用視圖查詢 18 select * from view_ScoreQuery

視圖裏面如果想用order by 排序,必須要加入top,所以視圖裏不建議加order by ,想要加order by 建議在調用視圖的時候加

T-SQL 視圖