奇怪的 SQL Server 视图定义

发布于 2024-12-02 19:28:59 字数 1098 浏览 0 评论 0原文

我“继承”了一个已有 10 多年历史的应用程序,它有时确实显示出它的年龄。我今天偶然发现了一个非常奇怪的视图定义 - 我似乎无法理解它!你能帮助我吗?这最初是在 SQL Server 7.0 上,后来被迁移到 SQL Server 2005 - 但显然它从未被重构/重做......

这是视图定义 - 基于一堆表和另一个视图:

CREATE VIEW dbo.MyOddView
AS
SELECT     
    t1.MVOID, t1.SomeOtherColumn, 
    t2.Number , 
    t3.OID, t3.FKOID,
    t4.AcctNo, 
    t5.ShortDesc, t5.ZipCode, t5.City, 
    t6.BankAcctNo
FROM
    dbo.viewFirst vf
INNER JOIN
    dbo.Table1 t1 ON vf.MVOID = t1.MVOID AND vf.ValidFrom = t1.ValidFrom 
LEFT OUTER JOIN
    dbo.Table2 t2 
RIGHT OUTER JOIN
    dbo.Table3 t3 ON t2.OID = t3.FKOID 
LEFT OUTER JOIN
    dbo.Table4 t4 ON t3.ZVOID = t4.OID 
LEFT OUTER JOIN
    dbo.Table5 t5
INNER JOIN
    dbo.Table4 t6 ON t5.OID = t6.BCOID 
    ON t4.ZVOID = t5.OID 
    ON t2.AddressOID = t4.OID
GO

我不知道的get 是两个 JOIN(对于 Table2 t2Table5 t5),它们旁边没有列出 JOIN 条件,另外两个视图定义末尾的 ON 条件 - 我似乎无法将其拆开并以“正确的”ANSI JOIN 语法将其重新组合在一起,以便我的行数相同...... ..(我原来的视图让我得到了超过 12'000 行的内容,并且第一次尝试重构它返回了一行……)

有什么想法吗?这到底是什么?对我来说这似乎是完全无效的 SQL - 但它似乎正在完成它的工作(并且在过去的几年里一直如此......)有什么想法吗?指针?

I've "inherited" a well over 10-year old app, and it does show its age at times. I've stumbled across a really weird view definition today - I just can't seem to make sense of it! Can you help me? This was originally on SQL Server 7.0 and has since been migrated to SQL Server 2005 - but obviously it's never been refactored / redone....

This is the view definition - based on a bunch of tables and another view:

CREATE VIEW dbo.MyOddView
AS
SELECT     
    t1.MVOID, t1.SomeOtherColumn, 
    t2.Number , 
    t3.OID, t3.FKOID,
    t4.AcctNo, 
    t5.ShortDesc, t5.ZipCode, t5.City, 
    t6.BankAcctNo
FROM
    dbo.viewFirst vf
INNER JOIN
    dbo.Table1 t1 ON vf.MVOID = t1.MVOID AND vf.ValidFrom = t1.ValidFrom 
LEFT OUTER JOIN
    dbo.Table2 t2 
RIGHT OUTER JOIN
    dbo.Table3 t3 ON t2.OID = t3.FKOID 
LEFT OUTER JOIN
    dbo.Table4 t4 ON t3.ZVOID = t4.OID 
LEFT OUTER JOIN
    dbo.Table5 t5
INNER JOIN
    dbo.Table4 t6 ON t5.OID = t6.BCOID 
    ON t4.ZVOID = t5.OID 
    ON t2.AddressOID = t4.OID
GO

What I don't get are the two JOIN's (for Table2 t2 and Table5 t5) which have no JOIN condition listed next to them, and the two extra ON conditions at the end of the view definition - I can't seem to rip this apart and put it back together in "proper" ANSI JOIN syntax so that my row count is the same...... (my original view gets me something over 12'000 rows, and a first attempt at refactoring this returned a single row......)

Any ideas? What the heck is this? Seems like totally invalid SQL to me - but it appears to be doing its job (and has been for the past several years....) Any thoughts? Pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

纸短情长 2024-12-09 19:28:59
SELECT ...
FROM   dbo.viewFirst vf
       INNER JOIN dbo.Table1 t1
         ON vf.MVOID = t1.MVOID
            AND vf.ValidFrom = t1.ValidFrom
       LEFT OUTER JOIN dbo.Table2 t2
                       RIGHT OUTER JOIN dbo.Table3 t3
                         ON t2.OID = t3.FKOID
                       LEFT OUTER JOIN dbo.Table4 t4
                         ON t3.ZVOID = t4.OID
                       LEFT OUTER JOIN dbo.Table5 t5
                                       INNER JOIN dbo.Table4 t6
                                         ON t5.OID = t6.BCOID
                         ON t4.ZVOID = t5.OID
         ON t2.AddressOID = t4.OID  

此语法在 Inside SQL Server 2008 T-SQL Querying 的第 7 章中介绍,或者参见 Itzik Ben Gan 的这篇文章以及后续文章Lubor Kollar 的信件

例如,t2.AddressOID = t4.OID 最后的 ON 子句意味着 JOIN 的 < code>t2 逻辑上最后发生。即首先逻辑处理其他连接,然后根据这些连接的结果进行LEFT JOIN

SELECT ...
FROM   dbo.viewFirst vf
       INNER JOIN dbo.Table1 t1
         ON vf.MVOID = t1.MVOID
            AND vf.ValidFrom = t1.ValidFrom
       LEFT OUTER JOIN dbo.Table2 t2
                       RIGHT OUTER JOIN dbo.Table3 t3
                         ON t2.OID = t3.FKOID
                       LEFT OUTER JOIN dbo.Table4 t4
                         ON t3.ZVOID = t4.OID
                       LEFT OUTER JOIN dbo.Table5 t5
                                       INNER JOIN dbo.Table4 t6
                                         ON t5.OID = t6.BCOID
                         ON t4.ZVOID = t5.OID
         ON t2.AddressOID = t4.OID  

This syntax is covered in chapter 7 of Inside SQL Server 2008 T-SQL Querying or see this article by Itzik Ben Gan and the follow up letter by Lubor Kollar

Having the ON clause for t2.AddressOID = t4.OID last for example means that the JOIN of t2 logically happens last. i.e the other joins are logically processed first then the LEFT JOIN happens against the result of those Joins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文