Solved

OLD sql server sql JOIN syntax

Posted on 2016-08-02
2
66 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

756 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