对视图创建索引

来源:互联网 发布:ubuntu snmp 编辑:程序博客网 时间:2024/06/04 20:09
提示 无法对视图创建 索引,因为该视图未绑定到架构
修改此问题 需要在 创建视图语句中加上 with SCHEMABINDING
create View myView(id,code) with SCHEMABINDING as select id,code from dbo.mytable
注意,表的表达式必须使用两段式 dbo.mytable 否则会报
"名称必须由两部分构成,并且对象不能引用自身。"
 sqlserver 2008 视图索引创建 在设计视图创建的话 默认 绑定到架构为否,TOP规范为是,这样再创建视图索引时就会出现视图未绑定到架构的错误信息,所以要在属性窗口改下 绑定到架构为是,TOP规范为否,还有就是count函数要变为COUNT_BIG函数;


创建视图索引首先要把视图绑定到架构
alter view view_prod_ProductProgress WITH SCHEMABINDING 
as
SELECT * from dbo.T1  as a inner join dbo.T2 as b on a.id=b.id
绑定到架构时,使用表要由两部分构成,就是 dbo.T1
 
接下来创建索引


if (exists (select * from sys.indexes where name = 'idx_prod_ProductProgress_Sht'))
    drop index view_prod_ProductProgress.idx_prod_ProductProgress_Sht
go
create index idx_prod_ProductProgress_Sht
on
view_prod_ProductProgress(ProductProgressSht);
报错:无法对视图 'view_prod_ProductProgress' 创建 索引。它没有唯一聚集索引。
所以首先要创建聚集索引


if (exists (select * from sys.indexes where name = 'idx_prod_ProductProgress_Sht'))
    drop index view_prod_ProductProgress.idx_prod_ProductProgress_Sht
go
create UNIQUE CLUSTERED index idx_prod_ProductProgress_Sht
on
view_prod_ProductProgress(ProductProgressSht);
0 0