且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除表后刷新视图

更新时间:2023-12-03 13:03:16

SQL Server 中的非物化视图可以被认为只是出现在该视图中的基础表之上的一个薄包装器.如果删除视图中涉及的一个或多个表,它将不起作用,因为无法再查询该表.这里有许多解决方法,其中之一就是创建一个索引(物化)视图:

A non materialized view in SQL Server can be thought of as just a thin wrapper on top of the underlying tables which appear in that view. If you drop one or more of the tables involved in the view, it won't work, because the table can no longer be queried. There are a number of workarounds here, one of which would be to just create an indexed (materialized) view:

CREATE VIEW View_tbl_one_series
WITH SCHEMABINDING
AS 
SELECT * from tbl1
UNION ALL
SELECT * from tbl2
UNION ALL
SELECT * from tbl3
UNION ALL
SELECT * from tbl4
GO
CREATE UNIQUE CLUSTERED INDEX idx ON View_tbl_one_series (id);

其他选项包括出于相同目的使用临时表,或者甚至是真正的常规表.

Other options would include using a temporary table for the same purpose, or maybe even a bona fide regular table.

请注意,通常在联合查询中执行 SELECT * 并不理想,因为它留下了列/列类型可能无法在联合中涉及的两个表之间正确对齐的可能性.

Note that in general doing SELECT * in a union query is not ideal, because it leaves open the possibility that the columns/column types may not line up properly between the two tables involved in the union.