SQL 带视图的服务器存储过程
SQL Server stored procedure with views
我使用存储过程更改了 table 中的一些条目,此更改会影响视图。
视图何时更新?存储过程什么时候开始工作(具体语句之后)或者什么时候完成?
我正在使用 SQL Server 2019 (v15)。
如果您对基础表进行了架构更改:
它不会自动发生。您必须专门调用 sp_refreshsqlmodule 来刷新视图
EXEC sp_refreshsqlmodule 'SchemaName.ViewName'
sp_refreshsqlmodule should be run when changes are made to the objects
underlying the module that affect its definition. Otherwise, the
module might produce unexpected results when it is queried or invoked.
To refresh a view, you can use either sp_refreshsqlmodule or
sp_refreshview with the same results.
您还可以使用 sp_refreshview 刷新视图以了解基础更改。
EXEC sp_refreshview N'SchemaName.ViewName'
If a view is not created with schemabinding, sp_refreshview should be
run when changes are made to the objects underlying the view that
affect the definition of the view. Otherwise, the view might produce
unexpected results when it is queried
通常最佳做法是使用 SCHEMA BINDING
选项创建视图,这将避免对基础 table 进行架构更改。 Benefits of Schemabinding
如果您对 tables 进行了数据更改,而不是架构更改:
View 只不过是一个存储的 SQL 语句。它是一个虚拟的 table。当您从视图 select 时,相应的 table 查询在 运行 时正在 运行。 View 没有单独存储数据。因此,无需更新或刷新视图。
我使用存储过程更改了 table 中的一些条目,此更改会影响视图。
视图何时更新?存储过程什么时候开始工作(具体语句之后)或者什么时候完成?
我正在使用 SQL Server 2019 (v15)。
如果您对基础表进行了架构更改:
它不会自动发生。您必须专门调用 sp_refreshsqlmodule 来刷新视图
EXEC sp_refreshsqlmodule 'SchemaName.ViewName'
sp_refreshsqlmodule should be run when changes are made to the objects underlying the module that affect its definition. Otherwise, the module might produce unexpected results when it is queried or invoked. To refresh a view, you can use either sp_refreshsqlmodule or sp_refreshview with the same results.
您还可以使用 sp_refreshview 刷新视图以了解基础更改。
EXEC sp_refreshview N'SchemaName.ViewName'
If a view is not created with schemabinding, sp_refreshview should be run when changes are made to the objects underlying the view that affect the definition of the view. Otherwise, the view might produce unexpected results when it is queried
通常最佳做法是使用 SCHEMA BINDING
选项创建视图,这将避免对基础 table 进行架构更改。 Benefits of Schemabinding
如果您对 tables 进行了数据更改,而不是架构更改:
View 只不过是一个存储的 SQL 语句。它是一个虚拟的 table。当您从视图 select 时,相应的 table 查询在 运行 时正在 运行。 View 没有单独存储数据。因此,无需更新或刷新视图。