SQL Server中的存储过程(按desc排序)?

时间:2010-02-26 10:50:40

标签: sql sql-server sql-server-2005 tsql stored-procedures

我有一个存储过程会提供最新记录,即按添加日期排序这是我的程序....

  select distinct top 5 videos.videoid,videos.videotitle,videos.videoname,
   convert(varchar,videos.posteddate,106) as  posteddate,videos.approvedstatus,
   videos.videoimage,(ISNULL(videos.views,0.0)) as [views],videos.privacy,
    (isnull(videos.rating,0.0)) as rating,videos.userid,
  users.userid,users.username from videos  left outer  join users on
videos.userid=users.userid
   where videos.approvedstatus='Y' and videos.privacy='P'
 order by  posteddate desc

但它没有提供最新记录

当我执行查询

select * from videos order by posteddate desc

它提供了准确的记录。但存储过程没有提供准确的记录。 你能帮助我吗,谢谢。


1 个答案:

答案 0 :(得分:4)

使用ORDER BY videos.posteddate

  select distinct top 5
     videos.videoid,
     videos.videotitle,
     videos.videoname,
     convert(varchar,videos.posteddate,106) as  posteddate,
     videos.approvedstatus,
     videos.videoimage,
     (ISNULL(videos.views,0.0)) as [views],
     videos.privacy,
     (isnull(videos.rating,0.0)) as rating,
     videos.userid,
     users.userid,users.username
  from
     videos
     left outer join
     users on videos.userid=users.userid
  where
     videos.approvedstatus='Y' and videos.privacy='P'
  order by
     videos.posteddate desc

您的原件是ORDER BY convert(varchar,videos.posteddate,106) DESC

的等效物

所以你按“dd mon yyyy”字符串排序,而不是所需的实际日期时间(yyyy-mm-dd hh等)

我猜这是SQL Server 2000:从内存SQL Server 2005将不会接受这种歧义