我有一个非常奇怪的问题,我可以重现该视图。我很确定某些视图元数据不是最新的并导致此问题...
简而言之:
- 我有一个视图(MyTestView)。
- 此视图已重命名 (MyTestViewOld)。
- 使用重命名视图的原始名称和不同的选择查询 (MyTestView) 创建一个新视图。
- sp_refreshview 在重命名的视图 (MyTestViewOld) 上执行。
- 现在 MyTestView 的实现就是 MyTestViewOld 的实现!
我尝试查看 SP sp_refreshview,但他只是执行了另一个名为 sp_refreshsqlmodule_internal 的 SP。我在主数据库的系统存储过程中没有看到这个视图:(
我的问题实际上是通过删除“MyTestViewOld”来解决的,但我想知道到底发生了什么!肿瘤坏死因子
这里是重现问题的脚本!(请按步骤执行)
--create a test table
create table dbo.MyTestContacts(
[Title] [varchar](20) NULL,
[Name] [varchar](255) NULL,
[FirstName] [varchar](255) NULL,
[Telephone] [varchar](50) NULL,
[Email] [varchar](255) NULL
)
--insert data in the temp table
insert dbo.MyTestContacts values ('Mr', 'Holly', 'Buddy', '0123456798', 'buddy@holly.co')
insert dbo.MyTestContacts values ('Mr', 'Valens', 'Ritchie', '987654312', 'ritchie@valens.co')
insert dbo.MyTestContacts values ('Mr', 'Richardson', 'Jiles Perry', '987654312', 'jp@richardson.co')
--create a view
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view dbo.MyTestView as
select Title, Name, FirstName from dbo.MyTestContacts
GO
--do a SCRIPT VIEW AS / ALTER TO in sql management studio to verify the view implementation
--rename the view
exec sp_rename 'dbo.MyTestView', 'MyTestViewOld'
--BIS1 (explained below; used in a 2nd test run)
--create a view with the same name as the first view, but different implementation!
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view dbo.MyTestView as
select Title, Name, FirstName, Telephone from dbo.MyTestContacts
GO
--do a SCRIPT VIEW AS / ALTER TO in sql management studio to verify the view implementation of MyTestView (telephone is added)
--perform a refreshView action on the MyTestViewOld view
exec sp_refreshview 'dbo.MyTestViewOld'
--do a SCRIPT VIEW AS / ALTER TO in sql management studio to verify the view implementation of MyTestView --> THIS SHOWS THE MyTestViewOld IMPLEMENTATION!!!!
--(BIS1) Note that this sequence gets broken when I do an ALTER VIEW on MyTestViewOld right after the rename!
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--alter view dbo.MyTestViewOld as
--select FirstName, Name, Email, Telephone from dbo.MyTestContacts
--GO
--drop everything
drop view dbo.MyTestViewOld
drop view dbo.MyTestView
drop table dbo.MyTestContacts