SQL Server:视图列数据类型在基础表数据类型被更改时没有更改吗?

时间:2023-01-21 16:55:27

If I have a table MyTable like this:

如果我有这样一张桌子:

Value1 decimal

Then I have a view MyView:

然后我有一个视图

Select SUM(Value1) as SumValue1 from MyTable

When I look at the view's column data type in SSMS under the view's columns section it is a decimal.

当我在视图列部分的SSMS中查看视图的列数据类型时,它是一个小数。

Now if I modify the table to:

现在,如果我将表修改为:

Value1 real

If I refresh the view's column section in SSMS the data type is still a decimal. Now if I open the view and resave it, the datatype becomes a float.

如果我刷新SSMS中视图的列部分,数据类型仍然是小数。现在,如果我打开视图并重新保存它,数据类型将变成一个浮点数。

Is this how it is supposed to be? It seems that the view doesn't change until I resave it.

是这样的吗?直到我重新保存它,视图才会改变。

1 个解决方案

#1


5  

Yes, this is expected. Tables can be changed in ways that make the views completely invalid. If you want to prevent this from happening, you can use the WITH SCHEMABINDING option when creating the view. From the linked article:

是的,这是预期。可以以使视图完全无效的方式更改表。如果您想防止这种情况发生,您可以在创建视图时使用WITH SCHEMABINDING选项。从链接的文章:

Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified.

将视图绑定到底层表或表的模式。当指定SCHEMABINDING时,不能以影响视图定义的方式修改基表或表。必须首先修改或删除视图定义本身,以删除要修改的表上的依赖项。

#1


5  

Yes, this is expected. Tables can be changed in ways that make the views completely invalid. If you want to prevent this from happening, you can use the WITH SCHEMABINDING option when creating the view. From the linked article:

是的,这是预期。可以以使视图完全无效的方式更改表。如果您想防止这种情况发生,您可以在创建视图时使用WITH SCHEMABINDING选项。从链接的文章:

Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified.

将视图绑定到底层表或表的模式。当指定SCHEMABINDING时,不能以影响视图定义的方式修改基表或表。必须首先修改或删除视图定义本身,以删除要修改的表上的依赖项。