CREATE View dbo.vw_Final WITH SCHEMABINDING
AS
SELECT id , SUM(Quantity) AS Quantity
FROM ( SELECT id, Quantity FROM Table1
UNION ALL
SELECT id, Quantity FROM Table2) AS T1
GROUP BY id
GO
CREATE UNIQUE CLUSTERED INDEX IDX_vw_Final_id
ON vw_Final(id);
GO
他就会有出错信息
Msg 10109, Level 16, State 1, Line 2
Cannot create index on view "dbo.vw_Final" because it references derived table "T1" (defined by SELECT statement in FROM clause). Consider removing the reference to the derived table or not indexing the view.
我理解就是用UNION产生的table不能index。那我应该怎么办呢?