Stored procedure in SQL Server (order by desc)?

我是研究僧i 提交于 2019-12-13 02:14:51

问题


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

but it is not giving latest records

when i execute the query

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.



回答1:


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

Your original is the equivalant of ORDER BY convert(varchar,videos.posteddate,106) DESC

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

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



来源:https://stackoverflow.com/questions/2340998/stored-procedure-in-sql-server-order-by-desc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!