Using TOP To Rank Columns In a Table

  • I'm not going to judge the merits of the problem. But I am going to point out a small but important thing that I see crop up frequently:

    '[' + COLUMN_NAME + ']'

    isn't a whole lot better than just COLUMN_NAME - what if they have a ']' in a column name ?

    QUOTENAME(COLUMN_NAME,'[')

    is much better

  • sanjay.patel 30254 (10/25/2011)


    What I would like to see is a source to destination dynamic data mapper, which is all referenced by using the dynaim columns as in the exmaple you have used, only extended to to also reference a second table, which agin the ordinal positions are sorted to relfect the data mapping, and then having further SQL conditions tagged on to allow a filter to be put in place.

    I want two tables, ordinal position to relfect the data mapping sequence, the source and destination columns and condition blocks may exist in either table, a dynamic process to put it all together, just the SQL part, don't want to execute it.....:-P

    Actually, this article gave you enough information to easily write your own. When you're done, you should write an article about it. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • A minor point - the spelling of 'dyanamic'

  • I think the article presents a good enough solution to a specific problem. If I ever have a need to select the TOP X columns from a table, then it would be an ad-hoc query I need right then and there, and I'd rather use someone elses code than waste time rolling my own solution. Obviously it's something that wouldn't belong in the application.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • My question is rather why do you want the top 10 columns rather then how

  • This just smacks of laziness on the developers part, to go to google and look up the API of their language's SQL Data Reader object. Pretty much every DB API allows you to select things based on column ordinal. C# will even allow you to get the name of the column back.

    Time for clue-bat application 😉

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply