Solved

OLD sql server sql JOIN syntax

Posted on 2016-08-02
2
80 Views
Last Modified: 2016-08-02
Hi All,
I am having issues converting the old syntax and wondered if any can convert the following to current SQL?

From APOPEN O, APPayments P, APNewPayables N Where O.ApplytoId =* P.ApplytoId  and N.voucherid =* P.applytoid

These are apparently RIGHT OUTER JOINS (i.e. RIGHT JOINS)

I have tried both the following and neither seems to work correctly. I suspect #2 (RIGHT JOIN) is closer than a LEFT join, but maybe the table (JOIN) order needs to be changed?

1. from APOPEN O LEFT JOIN APPayments P ON O.ApplytoId = P.ApplytoId LEFT JOIN APNewPayables N ON N.voucherid = P.applytoid
2. from APOPEN O RIGHT JOIN APPayments P ON O.ApplytoId = P.ApplytoId RIGHT JOIN APNewPayables N ON N.voucherid = P.applytoid

thank you
0
Comment
Question by:COACHMAN99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41740060
step 1 place each element on a new line

From APOPEN O
, APPayments P
, APNewPayables N
Where O.ApplytoId =* P.ApplytoId  
and N.voucherid =* P.applytoid

step 2 CUT the join condition to align with relevant table

From APOPEN O
, APPayments P O.ApplytoId =* P.ApplytoId  
, APNewPayables N N.voucherid =* P.applytoid
Where
and

step 3 tidy up

From APOPEN O
RIGHT OUTER JOIN APPayments P ON O.ApplytoId =* P.ApplytoId  
RIGHT OUTER JOIN APNewPayables N ON N.voucherid =* P.applytoid

step 4 remove those asterisks

From APOPEN O
RIGHT OUTER JOIN APPayments P ON O.ApplytoId = P.ApplytoId  
RIGHT OUTER JOIN APNewPayables N ON N.voucherid = P.applytoid

===============
personally I prefer
===============
while it makes no difference to the actual execution I prefer to ALWAYS refer to the PRIOR table FIRST

notice the bold here

From APOPEN O
RIGHT OUTER JOIN APPayments P ON O.ApplytoId = P.ApplytoId  

but on the next join

RIGHT OUTER JOIN APPayments P ON O.ApplytoId = P.ApplytoId  
RIGHT OUTER JOIN APNewPayables N ON N.voucherid = P.applytoid

SO, as a final step, get the join conditions consistent

From APOPEN O
RIGHT OUTER JOIN APPayments P ON O.ApplytoId = P.ApplytoId  
RIGHT OUTER JOIN APNewPayables N ON P.applytoid  = N.voucherid

I have previously attempt to explain my approach on "ye olde joins" here:
https://www.experts-exchange.com/articles/13757/Ye-Olde-Joins-how-to-replace-them.html

===========
  and finally
===========

There is an "unwritten convention" amongst many of use who write or maintain mountains of SQL. That convention is: always use left joins (& never use a right join)

Please don't get me wrong, you are completely free to use a right join and the SQL standards allow it. I am simply saying that you can always re-write the right join as a left join. by changing the order of how the tables are called, like this:

From APOPEN O
RIGHT OUTER JOIN APPayments P ON O.ApplytoId = P.ApplytoId  
RIGHT OUTER JOIN APNewPayables N ON P.applytoid  = N.voucherid

becomes

FROM APNewPayables  N
LEFT OUTER JOIN APPayments P  ON N.voucherid = P.applytoid
LEFT OUTER APOPEN O ON P.ApplytoId  = O.ApplytoId
1
 
LVL 7

Author Comment

by:COACHMAN99
ID: 41740070
Hi Paul
thanks for all the info.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question