Solved

using old-style left JOINs in SQL

Posted on 2014-11-01
9
227 Views
Last Modified: 2014-11-25
How does sql old syntax works when there is a third table called "Buyers" which I want to left join by OrderID and BuyerID?

SELECT o.OrderID, od.ProductID
  FROM dbo.Orders AS o, dbo.OrderDetails AS od
  WHERE o.OrderDate >= '20091001'
  AND o.OrderID = od.ProductID
left join Buyers on o.OrderID = BuyerID <<< ?
0
Comment
Question by:VBdotnet2005
[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
  • 5
  • 3
9 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 40417884
You probably can't go that far back (i.e. using =* or *= ) as most installations have that option turned off

The safer bet is to change the old fashioned inner join into an explicit inner join
SELECT
        O.OrderID
      , OD.ProductID
FROM dbo.Orders AS O
  INNER JOIN dbo.OrderDetails AS OD ON O.OrderID = OD.ProductID
  LEFT JOIN Buyers AS B ON O.OrderID = B.BuyerID
WHERE O.OrderDate >= '20091001'

Open in new window

0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 40418087
Try
SELECT o.OrderID, od.ProductID
  FROM dbo.Orders AS o inner join dbo.OrderDetails AS od
  on o.OrderDate >= '20091001'
  AND o.OrderID = od.ProductID
left join Buyers on o.OrderID = BuyerID 

Open in new window

0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 40419843
I don't think you have to convert the existing joins, just put the LEFT JOIN in its proper location, after the FROM clause and before the WHERE:

SELECT o.OrderID, od.ProductID, b.<column_name>
FROM dbo.Orders AS o, dbo.OrderDetails AS od
LEFT JOIN Buyers b on o.OrderID = b.BuyerID
WHERE o.OrderDate >= '20091001'
     AND o.OrderID = od.ProductID
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 40420746
I do not recommend mixing older and newer join syntax styles.

In a simple query you can get away with it, in more complex queries it can produce faults or errors.

Far better in my view, to adopt both the INNER JOIN and LEFT JOIN so the syntax style is consistent.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40421819
In theory, yes.  But if the existing code has a lot of inner joins that are done in the WHERE, you could easily introduce errors trying to switch to standard "INNER JOIN" syntax, esp. if you can't spend a lot of time on the changes.

New joins should always be done using the new syntax, esp. OUTER joins, as they can be ambiguous under the old syntax.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40422482
Not just in theory. The oft forgotten factor is sequence, which is vital in the newer syntax but very lax in the older syntax.

It is safer to adjust to newer syntax than to maintain awful hybrids.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40422678
All the more reason to rewrite only if you have the time to do it properly and test it fully.  Otherwise I say use the hybrid.  It may seem ugly to some, but it's vastly better than wrong results!
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40422742
There is a change as already, that requires testing. Substantially better to do this together,.

For this small and simple query there simply is no complexity or risk at adopring full new syntax.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40422753
Adopting. Not adopring.

Sorry
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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

738 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