我有三张桌子。你需要创建一个索引视图,你需要用WITH SCHEMABINDING
. 但是当我运行查询时,我得到一个错误。
表:
create table auction (
id uniqueidentifier default newid() primary key,
city nvarchar(max) not null,
auctioneer nvarchar(1000) not null
);
create table good (
id uniqueidentifier default newid() primary key,
name nvarchar(max) not null,
init_price decimal not null,
auction uniqueidentifier not null,
constraint fk_good_auction foreign key (auction) references auction(id)
on update no action
on delete no action
);
create table participant (
id uniqueidentifier default newid() primary key,
name nvarchar(max) not null,
auction uniqueidentifier not null,
constraint fk_participant_auction foreign key (auction) references auction(id)
on update no action
on delete no action
);
我要创建的视图。
create view superview
with schemabinding as
select a.city "city", a.auctioneer "auctioneer", g.name "good", g.init_price "init_price", p.name "participant"
from auction a join participant p on a.id = p.auction
join good g on a.id = g.auction;
尝试创建视图时出现错误 4512:
无法将视图“superview”绑定到架构,因为名称“拍卖”对于架构绑定无效。名称必须由两部分组成,并且对象不能引用自身。
名称应该由哪两部分组成?什么是指向自身的对象?
根据CREATE VIEW命令的文档
那些。模式绑定视图必须通过指定表的模式来访问表。
因此,如果表是在模式中创建的
dbo
,那么视图必须定义为关于
这意味着视图在其定义中不得引用自身。
假设没有
SCHEMABINDING
您可以创建这样的视图然后改变它,让它引用自己
之后使用它的真相不会变成事实,但尽管如此。
相同但使用
SCHEMABINDING
将给出与问题相同的错误消息 4512
它似乎是通用的,可以为
CREATE VIEW
两者返回ALTER VIEW
。