SQL Server中的存储过程(不同)?

时间:2010-02-26 05:33:52

标签: sql sql-server

我在SQL Server中编写了一个存储过程,就像这样

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 inner join users on
 users.userid=videos.userid where videos.approvedstatus='Y' and videos.privacy='P'
 order by videos.posteddate desc

但我得到这个错误你可以建议我

如果指定了SELECT DISTINCT,则ORDER BY项必须出现在选择列表中。

2 个答案:

答案 0 :(得分:4)

而不是Video.posteddate直接使用posteddate。

选择前5个视频.videoid,videos.videotitle,videos.videoname,    将(varchar,videos.posteddate,106)转换为posteddate,videos.approvedstatus,    videos.videoimage,(ISNULL(videos.views,0.0))as [views],videos.privacy,     (isnull(videos.rating,0.0))作为评级,videos.userid,   users.userid,users.username来自视频内部加入用户  users.userid = videos.userid其中videos.approvedstatus ='Y'和videos.privacy ='P'  按postsdate desc命令

答案 1 :(得分:1)

当您使用 distinct 时,将根据您的select语句选择唯一行。稍后在按列使用排序时进行查询时,列不在结果集中,因此您还需要在选择列表中添加缺少列。