Skip to content Skip to sidebar Skip to footer

Weird Sql Server View Definition

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!

Solution 1:

SELECT ...
FROM   dbo.viewFirst vf
       INNERJOIN dbo.Table1 t1
         ON vf.MVOID = t1.MVOID
            AND vf.ValidFrom = t1.ValidFrom
       LEFTOUTERJOIN dbo.Table2 t2
                       RIGHTOUTERJOIN dbo.Table3 t3
                         ON t2.OID = t3.FKOID
                       LEFTOUTERJOIN dbo.Table4 t4
                         ON t3.ZVOID = t4.OID
                       LEFTOUTERJOIN dbo.Table5 t5
                                       INNERJOIN 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.

Post a Comment for "Weird Sql Server View Definition"