Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234

T-SQL Parsing Crazy Eights Expand / Collapse
Author
Message
Posted Wednesday, September 30, 2009 1:44 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, December 4, 2013 3:54 PM
Points: 67, Visits: 230
Tom Garth (9/30/2009)
I agree with Tao that thoughtful aliasing, as opposed to A. B, C, will make code easier to read for the programmer, debugger, or DBA. Most who have been around a little while won't have to wonder what OH and OD reference. OH.customer_name doesn't present nearly the challenge to read as does SalesOrderHeader.customer_name.


I disagree. OH is much less meaningful than SalesOrderHeader. OH is not any easier or harder to read, but it means less. OH! It means OH, I forgot to name something.

Yes, you can create hugely long table names in an example to make the example look silly. But SalesOrderHeader is MUCH more meaningful than OH. OH could stand for the state of Ohio, or Old Hat, or OverHeat, or OrdersHandled, or OmitHeader, or OrdersHalted, or any one of a number of things. SalesOrderHeader does not have that problem. The table alias does NOT add anything meaningful, and it forces us to do an additional translation.

Yes, if you work with it all of the time, then you'll learn what OH stands for. And you'll learn what SOL stands for. SOL doesn't scream "line number" to me. The second programmer to come to the code sees SOL and thinks he's s*it out of luck figuring it out. Sales Order Line? Standing Order Link? Sales Omitted Late? WTF?

"Most who have been around a little while won't have to wonder what OH and OD reference." Bullsh!t. I have no idea what they reference.
Post #795995
Posted Wednesday, September 30, 2009 1:49 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, December 4, 2013 3:54 PM
Points: 67, Visits: 230
SELECT
StandingOrderLine.OrderNumber,
StandingOrderLine.LineNumber,
StandingOrderHeader.OrderTotal,
StandingOrderPayment.PaymentAmount
FROM dbo.StandingOrderLine INNER JOIN dbo.StandingOrderHeader
ON StandingOrderLine.CompanyID = StandingOrderHeader.CompanyID
AND StandingOrderLine.StandingOrderID = StandingOrderHeader.StandingOrderID
INNER JOIN StandingOrderPayment
ON StandingOrderHeader.CompanyID = StandingOrderPayment.CompanyID
AND StandingOrderHeader.StandingOrderID = StandingOrderPayment.StandingOrderID
WHERE StandingOrderLine.LineStatus = 'Shipped'

There, fixed that for you. Perfectly readable. The owner or schema names don't need to be repeated in the On clause or the Select column list since they are specified in the Join clause, and some line breaks help.

As for the Where clause, just like you did here, I always repeat the table name on purpose in the Where clause, even if it's a unique column name across the tables in the query, to help future programmers who might be looking at my code and not be completely familar with the tables. You could say "Where LineStatus = 'Shipped'" but I don't do that.
Post #795997
Posted Wednesday, September 30, 2009 3:35 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 25, 2014 6:37 AM
Points: 1,385, Visits: 1,242
To each his own, I guess!

To me, your "fix" makes it even harder to read (keeping a joined table on the same line as the first table? eek!) - but either way I'm still more interested in oddities of T-SQL parsing than your formatting preferences


http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
Post #796049
Posted Wednesday, September 30, 2009 3:43 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, December 4, 2013 3:54 PM
Points: 67, Visits: 230
I know, formatting preferences are a whole can of worms. But I think "From Table1 Inner Join Table2" makes a lot of sense. I put the third table on its own line with "Inner Join Table3".
Post #796057
Posted Thursday, October 29, 2009 5:22 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 11:58 AM
Points: 8,688, Visits: 9,221
A good fun questioon.

But I think it's a terrible pity that SQL Server is this forgiving - it would be better to treat such horrid, unsightly and unreadable code as an error.


Tom
Post #811299
Posted Thursday, October 29, 2009 5:43 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 7:45 PM
Points: 1,521, Visits: 3,037
Tom.Thomson (10/29/2009)
A good fun questioon.

But I think it's a terrible pity that SQL Server is this forgiving - it would be better to treat such horrid, unsightly and unreadable code as an error.
Thank you. You've hit on the real reason I thought it worth composing a QOD on this point. The rather loose syntactical rules can lead to results that are unexpected and difficult to explain.
Post #811304
Posted Tuesday, January 5, 2010 11:11 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, November 11, 2011 12:01 PM
Points: 158, Visits: 55
Nice question .. thought Provoking and had to try a number of times.

---- Live and Let Live! ----
Post #842266
Posted Friday, July 22, 2011 1:03 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 25, 2014 6:37 AM
Points: 1,385, Visits: 1,242
This is maybe slightly offtopic and a year late in coming, but I just fed this query in to my T-SQL formatter (I swear I didn't specifically prepare for this query - just stumbled across it again today), turns out it parses it correctly (i.e. in the same way the SQL Server engine does), and (somewhat) clarifies what's happening!

Link to the SQL formatted on PoorSQL.com

Sorry about the shameless self-promotion, but I was so chuffed (pleased) when I saw the output just now, I couldn't keep it to myself

Now I'm going to go ask John Arnott whether I can add this to my list of test cases in the project...


http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
Post #1146440
Posted Friday, July 22, 2011 9:42 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 7:45 PM
Points: 1,521, Visits: 3,037
Tao,
I'd be honored if you included my goofy query in your test bed. You've just given me two things in return: an Anglicism I'd not heard before ("chuffed") and a cool new tool in the formatter. For those, I suppose I can forgo asking a share of the royalties on the formatter.
Post #1146745
Posted Friday, July 22, 2011 9:46 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 25, 2014 6:37 AM
Points: 1,385, Visits: 1,242
Awesome thanks! I'll be linking the test scripts in as demo queries at some point over the next couple of days - having fun with the GitHub API.


http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
Post #1146755
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse