Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL Parsing Crazy Eights


T-SQL Parsing Crazy Eights

Author
Message
David Walker-278941
David Walker-278941
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 231
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.
David Walker-278941
David Walker-278941
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 231

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.
Tao Klerks
Tao Klerks
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1393 Visits: 1249
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.
David Walker-278941
David Walker-278941
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 231
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".
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10650 Visits: 11983
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

john.arnott
john.arnott
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1464 Visits: 3059
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.
jagadishrashmi
jagadishrashmi
SSC-Enthusiastic
SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)SSC-Enthusiastic (158 reputation)

Group: General Forum Members
Points: 158 Visits: 55
Nice question .. thought Provoking and had to try a number of times.

---- Live and Let Live! ----
Tao Klerks
Tao Klerks
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1393 Visits: 1249
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.
john.arnott
john.arnott
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1464 Visits: 3059
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. ;-)
Tao Klerks
Tao Klerks
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1393 Visits: 1249
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search