关于索引视图

chengpei_chen 2010-05-27 09:52:49
索引中用到了一个用户自定义函数,都绑定到架构了,为什么创建索引时还是报错,提示:
消息 10133,级别 16,状态 1,第 1 行
无法对视图 "Brand.dbo.xxxx" 创建 索引,因为视图引用的函数 "dbo.fn_CubicVolume" 在执行用户或系统数据访问。
...全文
264 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dla001 2010-05-28
  • 打赏
  • 举报
回复
学习学习。

索引视图要消耗系统资源的,曾经建过但发现维护代价比较高,所以放弃了。

chengpei_chen 2010-05-28
  • 打赏
  • 举报
回复
我现在函数也是确定的呀,输入相同的值返回同一个结果集,
用OBJECTPROPERTY来检验也是确定性函数,怎么还是不让建
chengpei_chen 2010-05-28
  • 打赏
  • 举报
回复
只要使用特定的输入值集并且数据库具有相同的状态,不管何时调用,始终都能范围相同结果的函数叫确定性函数。
我写的这个函数不算确定性函数吗
永生天地 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chengpei_chen 的回复:]
只返回固定值没有什么意义
[/Quote]
如果要建上索引,那列的数据必须是具有确定性的,否则sql server(任何。。。)无法为其创建索引。

难道将来会有动态索引。。。。。
chengpei_chen 2010-05-27
  • 打赏
  • 举报
回复
只返回固定值没有什么意义
chengpei_chen 2010-05-27
  • 打赏
  • 举报
回复
create function dbo.f_1 (@a varchar(10))
returns int
with schemabinding
as
begin
return convert(int,@a)
end

你这个函数中只返回一个固定值,你试一下这样:
create function dbo.f_1 (@a varchar(10))
returns int
with schemabinding
as
begin
declare @ttt varchar(111)

set @ttt=''
select @ttt= 字段 from dbo.table where 字段=@a
RETURN @ttt
end
永生天地 2010-05-27
  • 打赏
  • 举报
回复
全部成功
永生天地 2010-05-27
  • 打赏
  • 举报
回复

--1、
create table tb (id varchar(10))
insert tb
select '1' union all
select '10' union all
select '40'

--2、
create function dbo.f_1 (@a varchar(10))
returns int
with schemabinding
as
begin
return convert(int,@a)
end

--3、
create view v_tb1 with schemabinding
as
select dbo.f_1(id) as c from dbo.tb
go

--4、
create unique clustered index ix_tb1 on v_tb1(c)
chengpei_chen 2010-05-27
  • 打赏
  • 举报
回复
视图中带有函数的你们试没试过,试一试再说
chenguang79 2010-05-27
  • 打赏
  • 举报
回复
没看明白
--小F-- 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 feixianxxx 的回复:]
引用 1 楼 fredrickhu 的回复:

SQL code
---索引视图

索引视图是具体化的视图

--创建索引视图
create view 视图名 with schemabinding
as
select 语句
go

---创建索引视图需要注意的几点
1. 创建索引视图的时候需要指定表所属的架构
--错误写法
create view v_f with ……
[/Quote]

...SORRY
feixianxxx 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]

SQL code
---索引视图

索引视图是具体化的视图

--创建索引视图
create view 视图名 with schemabinding
as
select 语句
go

---创建索引视图需要注意的几点
1. 创建索引视图的时候需要指定表所属的架构
--错误写法
create view v_f with schemabinding
as
select
a.a,a.b,b……
[/Quote]

小F 这个你还没改过来啊。。
一样的写法
.在创建索引视图的select语句时,不使用*,必须指定具体的列名
--错误写法
create view v_f with schemabinding
as
select
a.a,a.b,b.a,b.b
from
dbo.a join dbo.b
on
a.id=b.id
go

---正确写法
create view v_f with schemabinding
as
select
a.a,a.b,b.a,b.b
from
dbo.a join dbo.b
on
a.id=b.id
go
chengpei_chen 2010-05-27
  • 打赏
  • 举报
回复
这个看过了,没用啊
--小F-- 2010-05-27
  • 打赏
  • 举报
回复
---索引视图

索引视图是具体化的视图

--创建索引视图
create view 视图名 with schemabinding
as
select 语句
go

---创建索引视图需要注意的几点
1. 创建索引视图的时候需要指定表所属的架构
--错误写法
create view v_f with schemabinding
as
select
a.a,a.b,b.a,b.b
from
a join b
on
a.id=b.id
go

---正确写法:
create view v_f with schemabinding
as
select
*
from
dbo.a join dbo.b
on
a.id=b.id
go


2.在创建索引视图的select语句时,不使用*,必须指定具体的列名
--错误写法
create view v_f with schemabinding
as
select
a.a,a.b,b.a,b.b
from
dbo.a join dbo.b
on
a.id=b.id
go

---正确写法
create view v_f with schemabinding
as
select
a.a,a.b,b.a,b.b
from
dbo.a join dbo.b
on
a.id=b.id
go

3.在创建索引视图的select 语句中,不能存在重复的列名,这个不举例了

4. 只能为索引视图创建唯一聚集索引
--正确的写法
create unique clustered index ix_uniquetb on v_tb
go

--错误的写法
create clustered index ix_uniquetb on v_tb
go

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧