exec sp_executesql N'
SELECT * FROM (
SELECT ROW_NUMBER() OVER (ORDER BY @strOrder) AS pos,@strGetFields
FROM [MemberAlbum] @strWhere) AS sp
WHERE pos BETWEEN (@PageIndex-1)*@PageSize+1) AND @PageIndex*@PageSize',
N'@strWhere varchar(250),@strOrder varchar(250),@strGetFields varchar(250), @PageIndex int,@PageSize int',
@strWhere='WHERE ID>10',@strOrder='EditTime DESC',@strGetFields='*',@PageIndex=1,@PageSize=20
'@strWhere' 附近有语法错误。
------解决方案--------------------
declare @tsql varchar(6000),@strWhere varchar(250),@strOrder varchar(250),@strGetFields varchar(250),@PageIndex int,@PageSize int
select @strWhere='WHERE ID>10',
@strOrder='EditTime DESC',
@strGetFields='*',
@PageIndex=1,
@PageSize=20
select @tsql='SELECT * FROM '
+' (SELECT ROW_NUMBER() OVER (ORDER BY '+@strOrder+') AS pos,'+@strGetFields
+' FROM [MemberAlbum] '
+ @strWhere+') AS sp '
+'WHERE pos BETWEEN '+rtrim((@PageIndex-1)*@PageSize+1)
+' AND '+rtrim(@PageIndex*@PageSize)
exec(@tsql)
------解决方案--------------------
如果要拼接 SQL,请拼好了再调用 sp_executesql,它的参数不能用来拼接 SQL 语句,只能是参数。