I just get a problem executing my sql statement, This is the Northwind db.
declare @PageIndex int
declare @PageSize int
declare @PageLowerBound int
declare @PageUpperBound int
declare @sql nvarchar(4000)
select @PageIndex = 3
select @PageSize = 5
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
with cts
as
(
SELECT *
FROM (SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS ROW,* FROM Orders)
AS Orders WHERE ROW >=@PageLowerBound AND Row<=@PageUpperBound
)
select @sql = @sql + 'select * from cte'
exec (@sql)
(Modified)
Just modify a little bit, I use select * from cte, I accidently pasted the wrong code. Sorry.
Actually, i am trying to append a where clause in @sql and execute it, but it throws a error message,saying “Incorrect syntax near the keyword ‘exec’.”
What did I do wrong?
Thank you
I’m guessing you are trying to make a standard CTE paging pattern (e.g. as described here) and make it dynamic for a pasted WHERE clause?
AFAIK you will also need to add the CTE into the sql string (exec or sp_executesql) in order to achieve this