我有以下SQL Server 2008查詢。我想擴充它,以便在填充@Filter參數時將該值傳遞給每個字段(跨列)。沒有篩選器,查詢就可以完美工作。我不確定過濾器是否應該放在我擁有它們的地方。任何幫助將非常感激:如果參數填充,則使用篩選器選擇ROW_NUMBER()?
ALTER PROCEDURE [dbo].[pd_Get_Team2]
@OutTotalRecCount INT OUTPUT,
@CurrentPage INT,
@PageSize INT,
@Token as nvarchar(50),
@UserID as nvarchar(50),
@SortDirection INT,
@SortField nvarchar(50),
@Filter nvarchar(50)
AS
SELECT * FROM
(SELECT ROW_NUMBER() OVER (
ORDER BY
CASE WHEN @SortDirection = 1 AND @SortField = 1 THEN pd_Role.UserID END ASC,
CASE WHEN @SortDirection = 2 AND @SortField = 1 THEN pd_Role.UserID END DESC
) AS Row,
pd_Role.id,
pd_Role.Edit_Rights,
pd_Role.UserID,
pd_Role.Is_Approved,
pd_Role.Login_Date,
pd_Role.Team
FROM pd_Role INNER JOIN
pd_Token ON pd_Role.Token = pd_Token.Token
WHERE
(pd_Token.Token = @Token) AND (pd_Token.UserID = @UserID)
--how do I structure a filter on these fields if the filter is populated?
--CASE WHEN LEN(@Filter)> 1
--AND pd_Role.Edit_Rights LIKE '%' + @Filter + '%' OR
--AND pd_Role.UserID LIKE '%' + @Filter + '%' OR
--AND pd_Role.Team LIKE '%' + @Filter + '%'
--
)
AS TeamWithRowNumbers
WHERE Row >= (@CurrentPage - 1) * @PageSize + 1 AND Row <= @CurrentPage*@PageSize
SELECT @OutTotalRecCount = COUNT(*) FROM pd_Role
WHERE (pd_Role.Token = @Token)
謝謝Serge!這很好用 – Rob 2013-04-23 14:58:22
請注意,這將不起作用if pd_Role.Edit_Rights,pd_Role.UserID,pd_Role.Team可以爲NULL – Serge 2013-04-23 15:14:11
它們永遠不會爲null,再次感謝! – Rob 2013-04-23 15:30:58