视图的优缺点及注意事项

使用视图的优点

1,视图着重于特定数据

2,简化数据的操作,易维护

使用视图的缺点

1,操作视图会比直接操作基础表要慢

2,修改限制

使用视图的注意事项

1,视图定义中的select语句不能包含下列内容

  order  by子句,除非在select语句的选择列中也有一个top子句

  一个top子句

扫描二维码关注公众号,回复: 6267843 查看本文章

  into关键字

  引用临时表或表变量

例子:

--视图的注意事项
/*
1,视图定义中的select语句不能包含下列内容

  order  by子句,除非在select语句的选择列中也有一个top子句

  一个top子句

  into关键字

  引用临时表或表变量
*/
--into,把数据从已存在的表中查询出来,添加到新表中,这个新表不存在
select * 
into newTable    --newTable不存在
from CommodityInfo

select * from newTable
go

create view vw_newTable2
as
    select * 
--into newTable2    --创建视图是不允许使用into,否则程序将会报错:create view必须是批处理中的仅有的语句
from CommodityInfo
go

--临时表
/*
    1,存储在tempdb
    2,本地临时表以“#”开头,全局临时表以“##”开头
    3,断开连接时临时表就被删除了
*/

--创建临时表
create table #newTable
( 
    id int,
    userName varchar(20)
)
go

--表变量
/*
1,表变量实际是变量一种形式
2,以@开头
3,存在内存中
*/
--创建表变量
declare @table table
(
    ID int,
    name varchar(20)
)
go
create view vw_Table
as
 select * from @table    --创建视图的时候是不允许使用表变量的,因为表变量会随着批处理的结束而结束,而创建视图又不能同其他的批处理共存。
 go

 create view vw_newTable  --不允许使用临时表创建视图
as
 select * from #newTable   
 go

猜你喜欢

转载自www.cnblogs.com/zhangxudong-cnblogs/p/10914797.html