September 30, 2009 at 1:44 pm
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.
September 30, 2009 at 1:49 pm
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.
September 30, 2009 at 3:35 pm
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.
September 30, 2009 at 3:43 pm
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".
October 29, 2009 at 5:22 pm
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
October 29, 2009 at 5:43 pm
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.
January 5, 2010 at 11:11 am
Nice question .. thought Provoking and had to try a number of times.
---- [font="Tahoma"]Live and Let Live![/font] ----
July 22, 2011 at 1:03 am
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.
July 22, 2011 at 9:42 am
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. 😉
July 22, 2011 at 9:46 am
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.
Viewing 10 posts - 31 through 39 (of 39 total)
You must be logged in to reply to this topic. Login to reply