INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Help converting deprecated joins

Help converting deprecated joins

(OP)
We are on SQL 2008 but our T-SQL is running at SQL 2000 compatibility level. I'm trying to convert some SQL to be compliant so we can move up the compatibility level. I'm having trouble with this one:

SQL 2000 compatibility level:

CODE -->

select *
From
	Employers e,
	Contact c,
	Location l,
	GroupEmployer ge,
	refAppStatus ras,
	AssociationSubAssociation asa,
	Association a
Where
	c.fkLocation =* l.pkLocation
and	l.fkID = e.Employerid
and	l.fkType = 'e'
and 	isnull(l.PrimaryLocation,0) = 1
and  	isnull(c.PrimaryContact,0)  = 1
and	e.employerid = ge.fkemployer
and	ge.fkAppStatus *= ras.pkAppStatus
and	ge.fkAssociationSubAssociation = asa.pkAssociationSubAssociation
and	asa.fkAssociation = a.pkAssociation
and	asa.EstExperienceYear = '2017' 

Notice the "c.fkLocation =* l.pkLocation" and "ge.fkAppStatus *= ras.pkAppStatus" joins. The result set contains 48,392 rows.

This is how I converted it:

CODE -->

select *
From
Employers e
join GroupEmployer ge on e.employerid = ge.fkemployer
join AssociationSubAssociation asa on ge.fkAssociationSubAssociation = asa.pkAssociationSubAssociation
join Association a on asa.fkAssociation = a.pkAssociation
join subassociation sa on sa.pksubassociation = asa.fksubassociation
join Location l on l.fkID = e.Employerid
left join Contact c on c.fkLocation = l.pkLocation
left join refAppStatus ras on ge.fkAppStatus = ras.pkAppStatus
left outer join Accountant acc  on e.fkAccountant = acc.pkAccountant
Where l.fkType = 'e'
and 	isnull(l.PrimaryLocation,0) = 1
and  	isnull(c.PrimaryContact,0)  = 1
and	asa.EstExperienceYear =  '2017' 

The result set contains 48,352 rows.

The difference is 40 rows and what separates them is a column called "c.PrimaryContact" and the difference is the first query contains forty extra rows with "c.PrimaryContact" as null. From what I see "c.fkLocation =* l.pkLocation" is a right outer join and is allowing null "c.PrimaryContact" rows even though the where specifiaclly says "and isnull(c.PrimaryContact,0) = 1".

I need the null "c.PrimaryContact" rows to be in the results. Just coding for them to be allow NULL "c.PrimaryContact" rows makes the counts much greater than the forty rows. Can someone see what I am missing? I've tried a number of variations, not shown here, but I can't get the queries to match the results set of the first one.


RE: Help converting deprecated joins

Try the below code. I found two issues with your attempt to make this code SQL-92 compliant.

The first is with one of the OUTER JOINs. You identified 'c.fkLocation =* l.pkLocation' as a LEFT JOIN and the operator (=*) listed is a RIGHT JOIN operatoer. I prefer to use LEFT JOIN only so I reversed the two columns and that should convert the join to be LEFT JOIN. That could clearly be messing with the results because I believe you are joining the table backwards.

The second is your rewrite of the query includes an additional table (Accountant) not found in the original query. Don't know if that was by mistake or not, but first remove that to see if that fixes the record count issue. It was listed as a LEFT JOIN and should not produce the results you are seeing, but it is best to line everything up before adding neew data points to ensure the new data points are not part of the problem.

I also made a couple other minor tweaks that are simply my personal preference, formatting for ease of reading and moving the additional JOIN filters into the JOIN clause instead of the WHERE clause.

This is typed and not tested. If this doesn't meet your needs, please supply a few sample records from each table so it is possible to see what the code is doing and how it needs to be adjusted to find the result set you are looking for.

CODE

SELECT
	e.*,
	ge.*,
	asa.*,
	a.*,
	sa.*,
	l.*,
	c.*,
	ras.*
FROM Employers e
INNER JOIN GroupEmployer ge
	ON e.employerid = ge.fkemployer
INNER JOIN AssociationSubAssociation asa
	ON ge.fkAssociationSubAssociation = asa.pkAssociationSubAssociation
		AND asa.EstExperienceYear = '2017'
INNER JOIN Association a
	ON asa.fkAssociation = a.pkAssociation
INNER JOIN subassociation sa
	ON sa.pksubassociation = asa.fksubassociation
INNER JOIN Location l
	ON l.fkID = e.Employerid
		AND l.fkType = 'e'
		AND ISNULL(l.PrimaryLocation, 0) = 1
LEFT JOIN Contact c
	ON l.pkLocation = c.fkLocation
		AND ISNULL(c.PrimaryContact, 0) = 1
LEFT JOIN refAppStatus ras
	ON ge.fkAppStatus = ras.pkAppStatus; 



Robert "Wizard" Johnson III
U.S. Military Vets MC
CSM, CSPO, MCPD, CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
Senior Database Developer

RE: Help converting deprecated joins

(OP)
That did it and thank you for the explanations. thumbsup2

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close