SQL Server 中的存储过程(按降序排序)? [英] Stored procedure in SQL Server (order by desc)?

查看:74
本文介绍了SQL Server 中的存储过程(按降序排序)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a stored procedure that will give the latest records i.e., order by added date this is my procedure....

  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

它给出了准确的记录.但存储过程没有给出确切的记录.你能帮我吗,谢谢.

it is giving exact records. but stored procedure is not giving exact records. can u help me, thank you.

推荐答案

Use 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 等)

So you are sorting by "dd mon yyyy" string, not the desired actual datetime (yyyy-mm-dd hh etc)

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

I guess this is SQL Server 2000: from memory SQL Server 2005 will not accept this ambiguity

这篇关于SQL Server 中的存储过程(按降序排序)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆