如果在创建后Test.MyView
更改了基表的结构,则可能会发生这种情况(例如:添加/删除列/删除并重新创建其他列[dbo].[TableTest]
)。你必须使用sp_refreshview
.
CREATE TABLE [dbo].[TableTest](
[ID] [int] NULL,
[Cost] [int] NULL,
[partnumber] [int] NULL
) ON [PRIMARY]
GO
CREATE SCHEMA Test
GO
CREATE TABLE [Test].[TableTest](
[source] [int] NULL,
[cost] [int] NULL,
[partnumber] [int] NULL
) ON [PRIMARY]
GO
CREATE VIEW [Test].[ViewTest] as select * from dbo.TableTest
GO
SELECT * FROM [Test].[ViewTest]
/*
ID Cost partnumber
----------- ----------- -----------
(0 row(s) affected)
*/
GO
DROP TABLE [dbo].[TableTest];
GO
CREATE TABLE [dbo].[TableTest](
[source] [int] NULL,
[cost] [int] NULL,
[partnumber] [int] NULL
) ON [PRIMARY]
GO
-- 从现在开始,Test.ViewTest 应该包括源、成本和零件编号列 -- 但是如果我们执行 SELECT * FROM [Test].[ViewTest] SQL Server 将显示相同的“旧”列
SELECT * FROM [Test].[ViewTest]
/*
ID Cost partnumber
----------- ----------- -----------
(0 row(s) affected)
*/
GO
-- 解决方案:sp_refreshview
EXEC sp_refreshview 'Test.ViewTest'
GO
SELECT * FROM [Test].[ViewTest]
/*
source cost partnumber
----------- ----------- -----------
(0 row(s) affected)
*/
GO