Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 6th, 2006, 04:13 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default select top problem

Hi,
I have the following line.
I want to select top 10 records order by date desc.i use ms access db.
but the query returns all the records and when I omit order by date
it works fine.
How is that??

   strSQL = "SELECT Top 10 NewsID,Headline,Hits FROM News Where Active= True order By DATE DESC"

 
Old January 6th, 2006, 04:36 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

A question about an Access database in the SQL Server forum.... Hmmmmm....

Anyway, Date is a reserved word so unless you really want to order by today's date, you need to rename the column and update the ORDER BY clause accordingly.

Not sure what Access returns more results, but I think that by ordering on DATE, all records are treated equally...

Just a guess though...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 6th, 2006, 05:20 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can also mtell the parser to not treat a keyword as a keyword by surrounding it with brackets:
Code:
SELECT Top 10 NewsID,Headline,Hits  FROM News Where Active= True order By [DATE] DESC
--

Joe (Microsoft MVP - XML)
 
Old January 6th, 2006, 05:36 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI,

First of all the Query listed is not so correct. How can you Order by a Column which is not mentioned in Select List ???

If you have mentioned in original Query and its still not working, you can go for a subquery ...

Select TOP 10 [Date], col1,col2..From
  ( Select TOP 100 PERCENT * From Tbl order By [Date] DESC )

Hope yuo got the point.....**Please check with the syntax for inner Query...I tried to put the idea only.


B. Anant
 
Old January 6th, 2006, 05:38 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by Anantsharma
 How can you Order by a Column which is not mentioned in Select List ???


Why not?

--

Joe (Microsoft MVP - XML)
 
Old January 7th, 2006, 07:11 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help.
The correct syntax was this:
strSQL= "Select TOP 10 Date,NewsID,Headline,Hits From ( Select TOP 10 date,NewsID,Headline,Hits From News order By Date DESC )"






Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT TOP n rgerald SQL Server 2000 3 May 12th, 2006 04:03 PM
SELECT TOP n NOT SELECTING TOP n! ibi SQL Language 8 March 30th, 2005 08:08 PM
SELECT TOP FROM HAVING khatfield29 SQL Language 1 August 23rd, 2004 02:41 PM
SELECT TOP !!!! Jane SQL Language 2 October 17th, 2003 11:22 AM
SELECT TOP ciko1973 SQL Language 5 August 8th, 2003 04:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.