无法创建索引视图
CREATE TABLE orders
(
order_no INT NOT NULL PRIMARY KEY,
prod_id INT NOT NULL,
quantity INT
);
CREATE VIEW product_stats WITH SCHEMABINDING
AS
SELECT a.prod_id, a.product_name,
(SELECT COUNT(*) FROM dbo.orders WHERE prod_id = a.prod_id) AS total FROM dbo.products a;
CREATE UNIQUE CLUSTERED INDEX [IDX_Order_Details_X]
ON product_stats (prod_id, total)
它抱怨说: 视图“product_stats”中的列“total”不能在索引或统计信息中使用,也不能用作分区键,因为它执行用户或系统数据访问。
数据库是MS SQL。
CREATE TABLE orders
(
order_no INT NOT NULL PRIMARY KEY,
prod_id INT NOT NULL,
quantity INT
);
CREATE VIEW product_stats WITH SCHEMABINDING
AS
SELECT a.prod_id, a.product_name,
(SELECT COUNT(*) FROM dbo.orders WHERE prod_id = a.prod_id) AS total FROM dbo.products a;
CREATE UNIQUE CLUSTERED INDEX [IDX_Order_Details_X]
ON product_stats (prod_id, total)
It complains:
Column 'total' in view 'product_stats' cannot be used in an index or statistics or as a partition key because it does user or system data access.
DB is ms sql.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
索引视图不能包含
COUNT(*)
或子查询。请参阅本文的“查看限制”部分。An indexed view cannot contain
COUNT(*)
or a subquery. See the "View Restrictions" section of this article.