I am trying to query without success, what I am trying to do is this:
ALTER PROCEDURE [dbo].[spPagination] -- ORDER BY id
@filterCol NVARCHAR(20) = NULL, --<<<<
@filterValue NVARCHAR(40) = NULL, --<<<<
@PageNumber INT,
@PageSize INT
AS
BEGIN
SET NOCOUNT ON;
SELECT
Emp.id , Emp.email, Emp.[firstName], Emp.[lastName], Emp.[salary],
Emp.[startDateWork], Emp.age, Rol.[name] AS Role
FROM
[dbo].tblEmployees5m Emp
INNER JOIN
[dbo].[tblRoles] Rol ON Emp.roleId = Rol.id
WHERE
@filterCol LIKE '%' + @filterValue + '%' --<<<<
ORDER BY
id
OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY OPTION (RECOMPILE);
SELECT COUNT(1) AS totalCount
FROM [dbo].tblEmployees5m
END
I am trying to apply filter to the result, according to @filterCol
and @filterValue
- if they are not NULL then I want to return the results with the where clause (which don't work now).
ELSE, if there 2 values are NULL
then don't apply the where clause. @filterCol
will hold the column name. @filterValue
will hold the column value.
How can I achieve that? there is a better way?
I suggested to use dynamic SQL and I read about it, but I don't understand what the benefit is that I will get from using it.. is it the right way?