更新 sql 视图时出现问题
我的 sql 视图有问题。我的实际观点包含几个连接,但出于我的问题的目的,我将用较小的示例来演示该问题。
假设我有观点……
create view A
as
select Id as IdC
from tableA
go
create view B
as
select b.Id,
b.Name,
a.*
from tableB b
inner join A a on a.Id = b.Id
go
那么一切都很好。然后我将视图 A 更改为读取...
alter view A
as
select Id as IdColumn
from tableA
go
所以现在如果我写...
select * from A
它会返回列 IdColumn
但是,如果我写...
select * from B
视图 A 中的原始 IdC
列名称
它会返回我尝试过的 sp_refreshview
,但这并没有帮助。
如何让视图 B 从视图 A 返回更新后的列名称?
更新**
好吧,我搞砸了原来的问题。我感谢大家的回应。我打算将视图A连接到视图B中的表B。看来视图B上的alter语句解决了这个问题。
I am having a problem with a sql view. My actual views encompass several joins, but for the purposes of my question I will demonstrate the issue with smaller examples.
Say I have the views…
create view A
as
select Id as IdC
from tableA
go
create view B
as
select b.Id,
b.Name,
a.*
from tableB b
inner join A a on a.Id = b.Id
go
So all is well. Then I change view A to read…
alter view A
as
select Id as IdColumn
from tableA
go
So now if I write…
select * from A
It returns column IdColumn
However, if I write…
select * from B
It returns the original IdC
column name from view A
I tried sp_refreshview
, but that has not helped.
How can I get view B to return the updated column name from view A?
UPDATE **
Well I messed up the original question. I thank everyone for their responses. I intend to join view A to table B in view B. It seems the alter statement on view B solves the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如我所看到的,您查询的是表A而不是视图A,
因此修改上述查询将解决您的问题
修改视图B的查询
As I can see you query you are refering tableA not view A
So modify the above query will resolve you issue
Modified query for view B
您的 viewB 正在加入 tableA,而不是视图 A,请尝试:
Your viewB is joining tableA, not view A, try:
这就是
select *
的问题如果您引用了视图 A
更改视图 A 返回的列(添加、删除、重命名)将不会反映在视图 B 中,直到您更改视图B. UDF 和存储过程中也会发生同样的情况。
That's the problem with
select *
If you would have referenced View A
Altering View A returned columns (add, delete, rename) will not be reflected in view B until you Alter view B. Same thing happens in UDF's and Stored Procedures.
视图 B 与视图 A 没有任何关系。
在视图 B 中,您有 TableA 和 TableB
尝试改为:
View B doesn't have anything to to with View A.
In View B you have TableA and TableB
Try instead: