SQL: is it possible to ORDER BY just the column number?

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default SQL: is it possible to ORDER BY just the column number?

    Hello,

    Is it possible to write an SQL statement that references a column number
    in a database and not the name of the column?

    Instead of this:
    set rs = oConn.Execute("SELECT * FROM table ORDER BY ID)
    I want to do this:
    set rs = oConn.Execute("SELECT * FROM table ORDER BY column(0) )

    Is it possible? Thanks!!

    Dragonhunter Guest

  2. Similar Questions and Discussions

    1. Column order
      Hi, I have a problem , I can't retreive the fields in the same order (like in the table) when I use the CFDump I have the column in a alphabetique...
    2. Auto generate unique order number sequential
      Hi: I need help on how to code in a CFFORM an order number. This order number must be auto generated, be unique and sequential. By the way I'm...
    3. multi column index and order by
      Hello, "order by a asc b desc" how can I create an index for this? Mage ---------------------------(end of...
    4. Column and line order
      If I have for example: 3 lines and 3 columns Data grid is constructed like this: first line: first column, second column, third colum second...
    5. OT. given x digits, how do I calculate the maximum permeations of number order ???
      ie, with the numbers 1,2,3 the permeations could be : 123 132 231 213 312 321 I learnt how to do this 20 years ago but now I need it, I...
  3. #2

    Default Re: is it possible to ORDER BY just the column number?

    > Is it possible to write an SQL statement that references a column number
    > in a database and not the name of the column?
    No. You can use this proprietary syntax, but don't count on it always
    working, and of course don't count on it working in other products.

    -- order by col1:
    SELECT col1, col2 FROM table ORDER BY 1

    -- order by col2 DESC:
    SELECT col1, col2 FROM table ORDER BY 2 DESC

    Are you really going to be using SELECT * in production code???


    Aaron Bertrand - MVP Guest

  4. #3

    Default Re: is it possible to ORDER BY just the column number?

    ORDER BY <column_number>

    is shown as ANSI standard SQL in my old SQL book ("Lan Times Guide to SQL")
    so it should work with most SQL DBMSs.

    --
    Mark Schupp
    --
    Head of Development
    Integrity eLearning
    Online Learning Solutions Provider
    [email]mschupp@ielearning.com[/email]
    [url]http://www.ielearning.com[/url]
    714.637.9480 x17


    "Aaron Bertrand - MVP" <aaron@TRASHaspfaq.com> wrote in message
    news:%23h6exdvfDHA.1088@TK2MSFTNGP10.phx.gbl...
    > > Is it possible to write an SQL statement that references a column number
    > > in a database and not the name of the column?
    >
    > No. You can use this proprietary syntax, but don't count on it always
    > working, and of course don't count on it working in other products.
    >
    > -- order by col1:
    > SELECT col1, col2 FROM table ORDER BY 1
    >
    > -- order by col2 DESC:
    > SELECT col1, col2 FROM table ORDER BY 2 DESC
    >
    > Are you really going to be using SELECT * in production code???
    >
    >

    Mark Schupp Guest

  5. #4

    Default Re: is it possible to ORDER BY just the column number?

    > ORDER BY <column_number>
    >
    > is shown as ANSI standard SQL in my old SQL book ("Lan Times Guide to
    SQL")
    > so it should work with most SQL DBMSs.
    That's column number in the SELECT list, not column number in the table. If
    you use SELECT *, you shouldn't be relying on the "order" of the columns to
    allow you to order by, IMHO.


    Aaron Bertrand [MVP] Guest

  6. #5

    Default Re: is it possible to ORDER BY just the column number?

    "Mark Schupp" <mschupp@ielearning.com> wrote in message news:<eVyFm8vfDHA.2576@TK2MSFTNGP11.phx.gbl>...
    > ORDER BY <column_number>
    >
    > is shown as ANSI standard SQL in my old SQL book ("Lan Times Guide to SQL")
    > so it should work with most SQL DBMSs.
    ORDER By <column_number> was valid in SQL-92, but not in SQL-99.

    In SQL-99, the sort order is specified as:
    <sort specification list>::=
    <sort specification>[ {<comma><sort specification>}...]

    <sort specification>::=
    <sort key>[ <ordering specification>] [<null ordering>]

    <sort key>::=<value expression>

    <ordering specification>::=
    ASC
    | DESC

    <null ordering>::=
    NULLS FIRST
    | NULLS LAST


    As you can see above, a <value expression> has replaced SQL-92's
    <column name> or <unsigned number>.

    Note that writing "ORDER BY 3" is valid SQL-99 syntax, but doesn't
    mean "order by the third column" as in SQL-92. My advice is: DO NOT
    USE "order by column-number". Sooner or later the applications using
    it just wont work like they used to. (No error code, just strange
    behavior.)


    Regards,
    Jarl
    Jarl Hermansson Guest

  7. #6

    Default Re: is it possible to ORDER BY just the column number?

    Good point Aaron, I don't use select * so it didn't occur to me.

    --
    Mark Schupp
    --
    Head of Development
    Integrity eLearning
    Online Learning Solutions Provider
    [email]mschupp2@ielearning.com[/email]
    [url]http://www.ielearning.com[/url]
    714.637.9480 x17


    "Aaron Bertrand [MVP]" <aaron@TRASHaspfaq.com> wrote in message
    news:eTs3bxzfDHA.2248@TK2MSFTNGP09.phx.gbl...
    > > ORDER BY <column_number>
    > >
    > > is shown as ANSI standard SQL in my old SQL book ("Lan Times Guide to
    > SQL")
    > > so it should work with most SQL DBMSs.
    >
    > That's column number in the SELECT list, not column number in the table.
    If
    > you use SELECT *, you shouldn't be relying on the "order" of the columns
    to
    > allow you to order by, IMHO.
    >
    >

    Mark Schupp Guest

  8. #7

    Default Re: is it possible to ORDER BY just the column number?

    why is select * so evil?

    -dragonhunter

    Mark Schupp wrote:
    > Good point Aaron, I don't use select * so it didn't occur to me.
    >
    > --
    > Mark Schupp
    > --
    > Head of Development
    > Integrity eLearning
    > Online Learning Solutions Provider
    > [email]mschupp2@ielearning.com[/email]
    > [url]http://www.ielearning.com[/url]
    > 714.637.9480 x17
    >
    > "Aaron Bertrand [MVP]" <aaron@TRASHaspfaq.com> wrote in message
    > news:eTs3bxzfDHA.2248@TK2MSFTNGP09.phx.gbl...
    > > > ORDER BY <column_number>
    > > >
    > > > is shown as ANSI standard SQL in my old SQL book ("Lan Times Guide to
    > > SQL")
    > > > so it should work with most SQL DBMSs.
    > >
    > > That's column number in the SELECT list, not column number in the table.
    > If
    > > you use SELECT *, you shouldn't be relying on the "order" of the columns
    > to
    > > allow you to order by, IMHO.
    > >
    > >
    Dragonhunter Guest

  9. #8

    Default Re: is it possible to ORDER BY just the column number?

    "Dragonhunter" <dragonhunter97@yahoo.com> wrote in message
    news:3F70A0E2.142BFEC4@yahoo.com...
    > why is select * so evil?
    [url]http://www.aspfaq.com/2096[/url]

    Ray at work


    Ray at Guest

  10. #9

    Default Re: is it possible to ORDER BY just the column number?

    [url]http://www.aspfaq.com/2096[/url]

    > why is select * so evil?


    Aaron Bertrand - MVP Guest

  11. #10

    Default Re: is it possible to ORDER BY just the column number?

    Dragonhunter wrote:
    > why is select * so evil?
    >
    > -dragonhunter
    >
    1. It practically guarantees that you will pull data across the wire that
    you don't really need, especially if your query involves joins.
    2. It can cause problems in your code if new columns are added to your
    tables.
    3. You can't control the order in which the columns are retrieved.

    Simply put, it's a bad, lazy habit. Selstar should be reserved for ad hoc
    one-off queries, say in Query Analyzer, not production code.

    Bob Barrows


    Bob Barrows Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139