SELECT
@TotalCount=count(1)
FROM TableA A WITH(NOLOCK)
INNER JOIN TableB B WITH(NOLOCK)
ON A.Id=B.Id
WHERE
A.Name=ISNULL(@name,A.Name)
AND
A.Age=ISNULL(@Age,A.Age)
AND
B.Address=ISNULL(@Address, B.Address)
AND
B.GoodsName like ‘%‘+ISNULL(@GoodsName,‘‘)+‘%‘
SELECT *
FROM
(
SELECT TOP(@EndNumber)
A.Name,
A.Age,
B.Address,
B.GoodsName,
ROW_NUMBER() OVER(ORDER BY A.CreationTime DESC) AS RowNumber
FROM TableA A WITH(NOLOCK)
INNER JOIN TableB B WITH(NOLOCK)
ON A.Id=B.Id
WHERE
A.Name=ISNULL(@name,A.Name)
AND
A.Age=ISNULL(@Age,A.Age)
AND
B.Address=ISNULL(@Address, B.Address)
AND
B.GoodsName like ‘%‘+ISNULL(@GoodsName,‘‘)+‘%‘
) a
where RowNumber>@StarNumber