SET @Sql = N' SELECT ItemID,'
+ N' RTRIM(Title) As ''Title'','
+ N' FriendsHit'
+ N' FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY ' + @Sort + N') As RowNumber'
+ N' FROM @Search)'
+ N' As ResultSet'
+ N' WHERE RowNumber BETWEEN (@CurrentPage-1)*@PageSize+1 AND @CurrentPage*@PageSize'
EXEC sp_executesql @Sql,
N'@Search As SearchTableType READONLY, @CurrentPage int, @PageSize int',
@Search = @Search,
@CurrentPage = @CurrentPage,
@PageSize = @PageSize
Needless to say your SearchTableType is declared previously as such:
CREATE TYPE SearchTableType AS TABLE
(
ID int primary key identity(1,1),
ItemID bigint NULL,
Title varchar(100) NULL,
FriendsHit varchar(max),
Ranking bigint NULL
)
-- Declare your table var inside your SP
DECLARE @Search As SearchTableType
Now you can go ahead and populate your table variable and pass it to your dynamic sql happily :-P
HTH