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'不能用于索引或统计信息或分区键,因为它可以进行用户或系统数据访问。
DB是ms sql。