更新 sql 视图时出现问题

发布于 2024-11-18 23:43:49 字数 823 浏览 6 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟沫凡尘 2024-11-25 23:43:49

正如我所看到的,您查询的是表A而不是视图A,

select b.Id, 
            b.Name, 
            a.*
    from tableB b 
    inner join tableA a on a.Id = b.Id

因此修改上述查询将解决您的问题

修改视图B的查询

 select b.Id, 
                b.Name, 
                a.*
        from tableB b 
        inner join A a on a.IdColumn = b.Id

As I can see you query you are refering tableA not view A

select b.Id, 
            b.Name, 
            a.*
    from tableB b 
    inner join tableA a on a.Id = b.Id

So modify the above query will resolve you issue

Modified query for view B

 select b.Id, 
                b.Name, 
                a.*
        from tableB b 
        inner join A a on a.IdColumn = b.Id
暮光沉寂 2024-11-25 23:43:49

您的 viewB 正在加入 tableA,而不是视图 A,请​​尝试:

inner join A a on a.Id = b.Id

Your viewB is joining tableA, not view A, try:

inner join A a on a.Id = b.Id
ゝ杯具 2024-11-25 23:43:49

这就是 select * 的问题

如果您引用了视图 A

create view B 
as
     select b.Id,
              b.Name,
              a.*
     from tableB b
     inner join A  on a.Id = b.Id 

更改视图 A 返回的列(添加、删除、重命名)将不会反映在视图 B 中,直到您更改视图B. UDF 和存储过程中也会发生同样的情况。

That's the problem with select *

If you would have referenced View A

create view B 
as
     select b.Id,
              b.Name,
              a.*
     from tableB b
     inner join A  on a.Id = b.Id 

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.

你爱我像她 2024-11-25 23:43:49

视图 B 与视图 A 没有任何关系。

在视图 B 中,您有 TableA 和 TableB

尝试改为:

alter view B
as
    select b.Id, 
            b.Name, 
            a.*
    from tableB b 
    inner join A a on a.IdColumn = b.Id
go

View B doesn't have anything to to with View A.

In View B you have TableA and TableB

Try instead:

alter view B
as
    select b.Id, 
            b.Name, 
            a.*
    from tableB b 
    inner join A a on a.IdColumn = b.Id
go
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文