 |
| 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
|
|
|
|

January 6th, 2006, 04:13 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"
|
|

January 6th, 2006, 04:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

January 6th, 2006, 05:20 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

January 6th, 2006, 05:36 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

January 6th, 2006, 05:38 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

January 7th, 2006, 07:11 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 )"
|
|
 |