Solved

OLD sql server sql JOIN syntax

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

732 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