Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Worst Practice - Sorting by Ordinal

By Andy Warren,

Worst practices - things that range from annoying you slightly to driving you to absolutely crazy. This one falls on the milder side (depending on how your week is going). Ever run across a query that looks like this?

Select field1, field2, field3 from pubs order by 1, 2

It really should look like this:

Select field1, field2, field3 from pubs order by field1, field2

I've heard it variously described as a power users tool (if they change the select they don't have to change the order by too) and a "shortcut" - meaning its faster to type 1, 2 than it is to spell out the field names. Give me a break!

Break is what you're liable to get when something changes. Maintenance work done on the select changes the field order, which changes the order by, which changes....how the indexes get used! This is one of those so subtle gotchas that can have you spend a lot of time looking for problems where none used to exist.

But wait...we could use our old friend Select *, then no one would have a reason to do maintenance work! We'll debate the merits of select * another day, for now consider that there are still users out there who insist on doing things like alphabetizing their column names in EM. You wind up with exactly the same problem. The order of the fields in the select changes, that changes

Interested in other Worst Practice articles? Follow this link. As always, I look forward to your comments on this article. Especially if you disagree! Nothing better for our readers than to see both sides of the story!

Total article views: 7378 | Views in the last 30 days: 3
 
Related Articles
FORUM

change collation field

change collation field

FORUM

Parameter Order-how change?

changing order of parametes to avoid "forward dependencies are not valid" error message

FORUM

SORTING with ORDER by for a FLOAT field

SORTING with ORDER by for a FLOAT field

FORUM

COLLATE order of NULL

change sorting order of NULL entries

FORUM

Change Size of Parameter Field

Change Size of Parameter Field

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones