Cannot create indexed view
CREATE TABLE orders
(
order_no INT N开发者_高级运维OT 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.
An indexed view cannot contain COUNT(*)
or a subquery. See the "View Restrictions" section of this article.
精彩评论