Solved

OLD sql server sql JOIN syntax

Posted on 2016-08-02
2
55 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
2 Comments
 
LVL 48

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

786 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