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

  1. #1

    Default Top in SQL

    I'm trying to use the "TOP" expression in SQL for Access. However, I
    get all of the records returned instead of the top 2 that I'm looking
    for. I don't get any errors either. It seems to be ignored. Here's
    the SQL I'm using:

    SELECT TOP 2 autoQuesID, fldQuesTitle
    FROM tblCNT_Question
    WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now() AND
    dtToRemove > Now() ) ORDER BY numOrder;

    There's 3 total rows in the table. The recordset returns all 3, which
    it should. However, I have the "TOP 2" phrase in the SQL and it still
    returns all 3 with no errors or anything.

    Any ideas?

    Thanks!
    GH Guest

  2. #2

    Default Re: Top in SQL

    GH wrote:
    > I'm trying to use the "TOP" expression in SQL for Access. However, I
    > get all of the records returned instead of the top 2 that I'm looking
    > for. I don't get any errors either. It seems to be ignored. Here's
    > the SQL I'm using:
    >
    > SELECT TOP 2 autoQuesID, fldQuesTitle
    > FROM tblCNT_Question
    > WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now()
    > AND dtToRemove > Now() ) ORDER BY numOrder;
    >
    > There's 3 total rows in the table. The recordset returns all 3, which
    > it should. However, I have the "TOP 2" phrase in the SQL and it still
    > returns all 3 with no errors or anything.
    >
    > Any ideas?
    >
    > Thanks!
    How many rows does this query return when you run it in Access using the
    Access Query Builder? If it returns 2 in that environment, then the problem
    is with the code in your asp page rather than the query itself. Needless to
    say, we can't help with that without seeing the code and, if you are using
    dynamic sql, the result of Response.Write sqlvariable.

    Bob Barrows

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  3. #3

    Default Re: Top in SQL

    Is numOrder distinct? If there are ties, it will return them...

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)




    "GH" <anonymous@discussions.nowhere.com> wrote in message
    news:uvOlamdaEHA.2792@TK2MSFTNGP09.phx.gbl...
    > I'm trying to use the "TOP" expression in SQL for Access. However, I
    > get all of the records returned instead of the top 2 that I'm looking
    > for. I don't get any errors either. It seems to be ignored. Here's
    > the SQL I'm using:
    >
    > SELECT TOP 2 autoQuesID, fldQuesTitle
    > FROM tblCNT_Question
    > WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now() AND
    > dtToRemove > Now() ) ORDER BY numOrder;
    >
    > There's 3 total rows in the table. The recordset returns all 3, which
    > it should. However, I have the "TOP 2" phrase in the SQL and it still
    > returns all 3 with no errors or anything.
    >
    > Any ideas?
    >
    > Thanks!

    Aaron [SQL Server MVP] Guest

  4. #4

    Default Re: Top in SQL

    Bob Barrows [MVP] wrote:
    > GH wrote:
    >
    >>I'm trying to use the "TOP" expression in SQL for Access. However, I
    >>get all of the records returned instead of the top 2 that I'm looking
    >>for. I don't get any errors either. It seems to be ignored. Here's
    >>the SQL I'm using:
    >>
    >>SELECT TOP 2 autoQuesID, fldQuesTitle
    >>FROM tblCNT_Question
    >>WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now()
    >>AND dtToRemove > Now() ) ORDER BY numOrder;
    >>
    >>There's 3 total rows in the table. The recordset returns all 3, which
    >>it should. However, I have the "TOP 2" phrase in the SQL and it still
    >>returns all 3 with no errors or anything.
    >>
    >>Any ideas?
    >>
    >>Thanks!
    >
    >
    > How many rows does this query return when you run it in Access using the
    > Access Query Builder? If it returns 2 in that environment, then the problem
    > is with the code in your asp page rather than the query itself. Needless to
    > say, we can't help with that without seeing the code and, if you are using
    > dynamic sql, the result of Response.Write sqlvariable.
    >
    > Bob Barrows
    >
    Well, I tried it in the Access Query Builder and I get three rows. It's
    almost as if it is complete ignoring the "Top 2" portion of the SQL
    statement. Any other ideas?

    Thanks again
    GH Guest

  5. #5

    Default Re: Top in SQL

    Aaron [SQL Server MVP] wrote:
    > Is numOrder distinct? If there are ties, it will return them...
    >
    Good call. I just removed the ORDER BY clause and it returned only two
    records. Here's how I modified the SQL statement that seems to be
    returning only two rows now:

    SELECT TOP 2 autoQuesID, fldQuesTitle
    FROM tblCNT_Question
    WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now() AND
    dtToRemove > Now() )
    ORDER BY numOrder,fldQuesTitle;

    Thanks Aaron
    GH Guest

  6. #6

    Default Re: Top in SQL

    GH wrote:
    > I'm trying to use the "TOP" expression in SQL for Access. However, I
    > get all of the records returned instead of the top 2 that I'm looking
    > for. I don't get any errors either. It seems to be ignored. Here's
    > the SQL I'm using:
    >
    > SELECT TOP 2 autoQuesID, fldQuesTitle
    > FROM tblCNT_Question
    > WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now() AND
    > dtToRemove > Now() ) ORDER BY numOrder;
    >
    > There's 3 total rows in the table. The recordset returns all 3, which
    > it should. However, I have the "TOP 2" phrase in the SQL and it still
    > returns all 3 with no errors or anything.
    >
    > Any ideas?
    >
    > Thanks!
    Call me crazy, im new here. but couldn't you do this?

    SELECT autoQuesID, fldQuesTitle
    FROM tblCNT_Question
    WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now() AND
    dtToRemove > Now() ) ORDER BY numOrder LIMIT 2;
    Dan Guest

  7. #7

    Default Re: Top in SQL

    Dan wrote:
    > GH wrote:
    >
    >> I'm trying to use the "TOP" expression in SQL for Access. However, I
    >> get all of the records returned instead of the top 2 that I'm looking
    >> for. I don't get any errors either. It seems to be ignored. Here's
    >> the SQL I'm using:
    >>
    >> SELECT TOP 2 autoQuesID, fldQuesTitle
    >> FROM tblCNT_Question
    >> WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now()
    >> AND dtToRemove > Now() ) ORDER BY numOrder;
    >>
    >> There's 3 total rows in the table. The recordset returns all 3,
    >> which it should. However, I have the "TOP 2" phrase in the SQL and
    >> it still returns all 3 with no errors or anything.
    >>
    >> Any ideas?
    >>
    >> Thanks!
    > Call me crazy, im new here. but couldn't you do this?
    >
    > SELECT autoQuesID, fldQuesTitle
    > FROM tblCNT_Question
    > WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now()
    > AND dtToRemove > Now() ) ORDER BY numOrder LIMIT 2;
    No. JetSQL does not support the LIMIT keyword.

    Bob Barrows
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows [MVP] Guest

  8. #8

    Default Re: Top in SQL

    > Call me crazy, im new here. but couldn't you do this?

    You're crazy! Did you try that query in Access?
    >
    > SELECT autoQuesID, fldQuesTitle
    > FROM tblCNT_Question
    > WHERE ( blnLive = TRUE AND blnHmpgNews = TRUE AND dtToPost <= Now() AND
    > dtToRemove > Now() ) ORDER BY numOrder LIMIT 2;

    Aaron [SQL Server MVP] 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