select top @varible

來源:互聯網
上載者:User

        在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.