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

T-SQL 檢視

 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 建議在呼叫檢視的時候加