我有分页的存储过程和我hava paramter @where我需要将它传递给我的SP,如下所示
Create PROCEDURE SP_hrm_Employee_Paged
(
@PageNo INT = 1,
@PageSize INT = 10,
@where nvarchar(500)
)
AS
BEGIN
/*–Declaring Local Variables corresponding to parameters for modification */
DECLARE
@lPageNbr INT,
@lPageSize INT,
@lFirstRec INT,
@lLastRec INT,
@lTotalRows INT
/*Setting Local Variables*/
SET @lPageNbr = @PageNo
SET @lPageSize = @PageSize
SET @lFirstRec = ( @lPageNbr - 1 ) * @lPageSize
SET @lLastRec = ( @lPageNbr * @lPageSize + 1 )
SET @lTotalRows = @lFirstRec - @lLastRec + 1
; WITH CTE_Results
AS (
SELECT ROW_NUMBER() over(ORDER By BranchId ASC) as ROWNUM,
Count(*) over () AS TotalCount,
EmployeeId,
EmployeeFirstName,
EmployeeMiddleName,
EmployeeLastName,
EmployeeMobile,
EmployeeMobile2,
EmployeeEmail,
EmployeeAddress
FROM t_hrm_employees
)
SELECT
TotalCount,
ROWNUM,
EmployeeId,
EmployeeFirstName,
EmployeeMiddleName,
EmployeeLastName,
EmployeeMobile,
EmployeeMobile2,
EmployeeEmail,
EmployeeAddress
FROM CTE_Results AS CPC
WHERE
ROWNUM > @lFirstRec
AND ROWNUM < @lLastRec
ORDER BY ROWNUM ASC
END
答案 0 :(得分:1)
您可以使用SP_EXECUTESQL这样的动态sql -
但在请注意之前 - 请勿使用&#34; sp _&#34;来启动您的SP名称。前缀,因为它为sql server中的系统存储过程保留。
CREATE PROCEDURE usp_hrm_Employee_Paged (
@PageNo INT = 1
,@PageSize INT = 10
,@where NVARCHAR(500)
)
AS
BEGIN /*–Declaring Local Variables corresponding to parameters for modification */
DECLARE @lPageNbr INT
,@lPageSize INT
,@lFirstRec INT
,@lLastRec INT
,@lTotalRows INT
/*Setting Local Variables*/
SET @lPageNbr = @PageNo
SET @lPageSize = @PageSize
SET @lFirstRec = (@lPageNbr - 1) * @lPageSize
SET @lLastRec = (@lPageNbr * @lPageSize + 1)
SET @lTotalRows = @lFirstRec - @lLastRec + 1;
DECLARE @ExequteSql NVARCHAR(MAX)
SET @ExequteSql = N'
WITH CTE_Results
AS (
SELECT ROW_NUMBER() OVER (
ORDER BY BranchId ASC
) AS ROWNUM
,Count(*) OVER () AS TotalCount
,EmployeeId
,EmployeeFirstName
,EmployeeMiddleName
,EmployeeLastName
,EmployeeMobile
,EmployeeMobile2
,EmployeeEmail
,EmployeeAddress
FROM t_hrm_employees
WHERE ' + @where + '
)
SELECT TotalCount
,ROWNUM
,EmployeeId
,EmployeeFirstName
,EmployeeMiddleName
,EmployeeLastName
,EmployeeMobile
,EmployeeMobile2
,EmployeeEmail
,EmployeeAddress
FROM CTE_Results AS CPC
WHERE ROWNUM > @lFirstRec
AND ROWNUM < @lLastRec
ORDER BY ROWNUM ASC'
EXEC SP_EXECUTESQL @ExequteSql ,N'@lFirstRec INT, @lLastRec INT' ,@lFirstRec = @lFirstRec ,@lLastRec = @lLastRec
END