Solved

OLD sql server sql JOIN syntax

Posted on 2016-08-02
2
46 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
Comment Utility
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
Comment Utility
Hi Paul
thanks for all the info.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
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 different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now