--视图:就是一张虚拟表,本质上就是存储了一条查询语句的代码块。它就可以像操作表一样自由的操作视图
select * from vw_Test where StudentResult>70
--代码创建视图
语法:
--go
--create view vw_名称
--as
-- 一条查询语句
--go
if exists(select * from sysobjects where name=‘vw_GetPageData‘)
drop view vw_GetPageData
go
create view vw_GetPageData
as
select row_number() over(order by studentno) sid,* from student
--select * from Classes --只能是一条Sql查询语句
--delete from student where studentno>100 --不能创建增加删除和修改语句
go
select * from vw_GetPageData where ID>=1 and id<=5
if exists(select * from sysobjects where name=‘vw_viewopt‘)
drop view vw_viewopt
go
create view vw_viewopt
as
--如果在视图中进行排序,那么就需要同时也使用top
select top (select COUNT(*) from Student) Student.StudentNo,Student.StudentName,classname from Classes inner join Student on Classes.ClassId=Student.ClassId order by StudentName
go
--对视图执行类型表的操作
select * from vw_viewopt order by studentname
delete from vw_viewopt where classid=19
--因为修改会影响多个基表:如果操作影响了多张表,那么操作就会报错
update vw_viewopt set studentname=‘刘健‘ ,classname=‘二期班123‘ where StudentNo=1