日期:2014-05-18  浏览次数:20354 次

SQL 存储过程 NOT IN 的问题。
SQL code
ALTER     PROCEDURE sArticle
  @currentPage int 
AS
declare @sql1 varchar(250)
declare @sql2 varchar(250) 

set @sql1 ='select top 20 ID,Topic,Author,AddTime,ViewNum from Article order by ID desc'
set @sql2 ='select top 20 ID,Topic,Author,AddTime,ViewNum from Article where ID not in ([color=#FF0000]select top (@currentPage-1)*20 ID from Article order by ID desc[/color]) order by ID desc'
if (@currentPage=1) 
  execute (@sql1)
else
  execute (@sql2)


Not IN (1,2,3,4 ...) 上面红色的语句,怎样写才是正确的?

------解决方案--------------------
TOP 后不能跟一个变量。
------解决方案--------------------
你是2000还是05?加()就行了吧

SQL code
ALTER     PROCEDURE sArticle
  @currentPage int 
AS
declare @sql1 nvarchar(250)
declare @sql2 nvarchar(250) 

set @sql1 ='select top 20 ID,Topic,Author,AddTime,ViewNum from Article order by ID desc'
set @sql2 ='select top 20 ID,Topic,Author,AddTime,ViewNum from Article where ID not in (select top ((@currentPage-1)*20) ID from Article order by ID desc) order by ID desc'
if (@currentPage=1) 
  execute (@sql1)
else
 exec sp_executesql @sql2,N'@currentPage int',@currentPage