sql server - How does this SQL syntax work? -
this question has answer here:
- odd inner join syntax , encapsulation 1 answer
- join statement order of operation 1 answer
i came across horrifyingly freakish sql query today developer generated sql server query designer tool. i hate query designer, i'm stuck trying figure out did. i've never seen syntax before , don't understand it. how work?
in particular is multiple on clauses joined separate join clauses throwing me off.
select * dbo.tbldealstatus right outer join dbo.tbluser right outer join dbo.tblownerlocation inner join dbo.tblowner inner join dbo.tbldeal on dbo.tblowner.ownerid = dbo.tbldeal.ownerid on dbo.tblownerlocation.dealid = dbo.tbldeal.dealid on dbo.tbluser.userid = dbo.tbldeal.chk_contact left outer join dbo.tblcompany tblcompany_1 inner join dbo.tblparticipation on tblcompany_1.companyid = dbo.tblparticipation.companyid on dbo.tbldeal.participationid = dbo.tblparticipation.participationid on /*... ....so on , forth...*/
first, make rule clarity never mix right , left joins in same query. right joins can switched left joins , alone make easier figure out going on.
next abandon select *. never appropraite in query joins returning same data in 2 or more fields (the join fields) , wasteful of valuable network , database processing time.
i believe wierd ons forcing query go in particular order. bad , should not used in opinion hard maintain , hard developers understand not common , totally unneeded. reversing right joins , putting tables in order need join them may fix this. if not may need few derived tables right data. note in reversing it, may need change inner joins else. right such mess, highly not return correct results. in rewriting it, while want see if changes change results, want use judgement determine if changes fixes bad query or incorrect changes translate query maintainable.
if developer wrote mess still there, force him rewrite in in more standard sql , tell him forbidden ever use query designer again. fails code review far concerned.
if rewriting this, table should first in query , work down there. personal guess right tbldeal table don't know data model wrong.
Comments
Post a Comment