select top @varible

        在MS SQL Server 2K5中寫的一個存儲過程如下:
CREATE PROCEDURE [dbo].[GetRecentNews]
     @SchoolID int,
     @NewsType int,
     @PageSize tinyint,
     @Page int
AS
 --分頁
 if(NewsType=0)
     SELECT TOP @PageSize ID,Title,UpdateTime FROM News WHERE  SchoolID=@SchoolID AND ID NOT IN
  (SELECT TOP @PageSize*(@Page-1) ID FROM News WHERE SchoolID=@SchoolID  ORDER BY UpdateTime DESC)  ORDER BY UpdateTime DESC  
 else
     SELECT TOP @PageSize ID,Title,UpdateTime FROM News WHERE  SchoolID=@SchoolID AND NewsType=@NewsType AND ID NOT IN
  (SELECT TOP @PageSize*(@Page-1) ID FROM News WHERE SchoolID=@SchoolID AND NewsType=@NewsType ORDER BY UpdateTime DESC)  ORDER BY UpdateTime DESC

   RETURN
GO


      在SQL Server 2K5中沒有問題,但到了Sql Server 2K中,卻報錯:
服務器: 消息 170,級別 15,狀態 1,過程 GetRecentNews,行 9
第 9 行: '@PageSize' 附近有語法錯誤。
服務器: 消息 170,級別 15,狀態 1,過程 GetRecentNews,行 10
第 10 行: '@PageSize' 附近有語法錯誤。
服務器: 消息 170,級別 15,狀態 1,過程 GetRecentNews,行 13
第 13 行: '@PageSize' 附近有語法錯誤。

     暈!Sql Server 2K中寫法如下:
CREATE PROCEDURE [dbo].[GetRecentNews]
 @SchoolID int,
 @NewsType int,
 @PageSize int,
 @Page int
AS
 --分頁
 declare @current int
 set @current= @PageSize*(@Page-1)
 if (@NewsType=0)
     exec('SELECT TOP ' + @PageSize +' ID,Title,UpdateTime FROM News WHERE  SchoolID='+ @SchoolID +' AND ID NOT IN
  (SELECT TOP ' + @current +' ID FROM News WHERE SchoolID='+@SchoolID+' ORDER BY UpdateTime DESC)
  ORDER BY UpdateTime DESC')
 else
     exec('SELECT TOP ' + @PageSize +' ID,Title,UpdateTime FROM News WHERE  SchoolID='+ @SchoolID +' AND NewsType='+ @NewsType +' AND ID NOT IN
  (SELECT TOP ' + @current +' ID FROM News WHERE SchoolID='+@SchoolID+' AND NewsType='+@NewsType+' ORDER BY UpdateTime DESC)
  ORDER BY UpdateTime DESC')
 return
GO

發佈了58 篇原創文章 · 獲贊 1 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章