存储过程之------排序篇

CREATE PROCEDURE HVC_Statistics_GetPortalRankByWeek
(
 @Week int,
 @Rownum int,
 @SortField int 
)

AS
DECLARE @SQL VARCHAR(1000)


SET @SQL =
'SELECT TOP ' +CAST(@Rownum AS VARCHAR(20))+' PortalName=(Select PortalName From Portals WHERE Portals.PortalID = A.PortalID),*

FROM HVC_Statistics_PortalRank A

WHERE Week = ' +CAST(@Week AS VARCHAR(20))+' Order By CASE ' +CAST(@SortField AS VARCHAR(20))+
    '  WHEN ' + '''  0  ''' +'THEN' + '  ResultValue' +
    '  WHEN ' + '''  1  ''' +'THEN' + '  TotalAccessed' +
    '  WHEN ' + ''' 2 ''' +'THEN' + '  AllArticlesNum' +
    ' WHEN ' +''' 3 '''+ ' THEN ' +'AllArticleHits'+
    ' WHEN ' +'''4 ''' +'  THEN ' +' MonthTotalUpdated ' +
    ' WHEN'  + '''5 '''+ ' THEN ' + ''' MonthArticleHits'''+
    ' WHEN '  + '''6 ''' + '  THEN ' +'  WeekArticleUpdated'+
    ' WHEN ' + ''' 7'''+ ' THEN '+'  WeekArticleHits '+
    ' WHEN '  +''' 8 ''' +' THEN ' +'  Score ' +
    ' ELSE  ResultValue  END DESC'

EXEC(@SQL)
--PRAINT(@SQL)
RETURN
GO

你可能感兴趣的:(存储过程)