我正在尝试创建一个从表employee中搜索员工的存储过程。
我的查询看起来像这样
ALTER PROCEDURE [dbo].[EmployeeList]
@CurrentPageNo INT = null,
@PageSize INT = 5,
@FirstName VARCHAR(100),
@Email VARCHAR(100),
@DeptId int = null
AS
BEGIN
DECLARE @StartRecordNo INT, @EndRecordNo INT
IF @CurrentPageNo IS NULL
SET @CurrentPageNo = 1
SET @StartRecordNo = ((@CurrentPageNo - 1) * @PageSize)
SET @EndRecordNo = @StartRecordNo + @PageSize
SELECT
ROW_NUMBER() OVER (ORDER BY CreatedByDate desc) As srno,
EmployeeID, FirstName, LastName, Email, DateOfBirth, Joining,
MobileNumber, MonthsOfExperience, TotalExperience, DepartmentName
FROM
Employee
INNER JOIN
Department ON Employee.DeptId = Department.DepartmentID
WHERE
(ISNULL(@FirstName, '') = '' OR Employee.FirstName LIKE '%' + FirstName + '%')
AND
(ISNULL(@Email, '') = '' OR Employee.Email LIKE '%' + @Email + '%')
AND
(@DeptId IS NULL OR Employee.DeptId = @DeptId)
END
该过程已成功编译。但是当执行它时
exec EmployeeList 1,10,'shaili','',null
它也显示其他记录。有谁能解释一下?我只想要具有给定名称的记录。
结果我得到了:
答案 0 :(得分:2)
#Siddhant only little mistake by you. i.e you pass FirstName instead of @FirstName. check below code.
ALTER PROCEDURE [dbo].[Test_EmployeeList]
@CurrentPageNo INT = null,
@PageSize INT=5,
@FirstName VARCHAR(100),
@Email VARCHAR(100),
@DeptId int = null
AS
BEGIN
DECLARE @StartRecordNo INT,@EndRecordNo INT
IF @CurrentPageNo IS NULL
SET @CurrentPageNo=1
SET @StartRecordNo=((@CurrentPageNo-1) * @PageSize)
SET @EndRecordNo=@StartRecordNo + @PageSize
SELECT ROW_NUMBER()OVER(ORDER BY Reg_Date desc) As srno,
F_Name, L_Name, Email, Mobile, Reg_Date from tbl_LoginMaster
inner join tbl_Login_Detail
ON tbl_LoginMaster.User_Id = tbl_Login_Detail.User_Id
WHERE (ISNULL(@FirstName,'')='' OR tbl_LoginMaster.F_Name LIKE '%'+@FirstName+'%') AND
(ISNULL(@Email,'')='' OR tbl_LoginMaster.Email LIKE '%'+@Email+'%') AND
(@DeptId is null OR tbl_LoginMaster.User_Id = @DeptId )
END
GO
exec Test_EmployeeList 1,10,'Mohit','',null