如何选择所有记录?

时间:2016-03-13 15:42:45

标签: sql sql-server sql-server-2008 tsql

我有一个填充了数据的gridview,它只通过调用此SP来填充用户登录的数据。

ALTER PROCEDURE [Employee].[usp_getAllEmployeesReliefs]

    @EmployeeID INT

AS
BEGIN

    SELECT EmployeeReliefID,
           Amount,
           [Employee].[Relief].IsActive,
           PersonalInfo.PersonalInfoName,
           [Setup].Reliefs.Name AS ReliefName
    FROM [Employee].[Relief]
    INNER JOIN PersonalInfo ON PersonalInfo.pk_PersonalInfo_ID = Employee_ID
    INNER JOIN [Setup].Reliefs ON [Setup].Reliefs.ReliefID = [Employee].[Relief].Relief_ID
    WHERE employeeID = @EmployeeID
    ORDER BY EmployeeReliefID DESC

END

但我希望显示向员工提供的记录以及未分配给员工的记录。怎么做 ?

1 个答案:

答案 0 :(得分:0)

你可以这样: - 更改参数以具有默认值 - 将您的where子句更改为(employeeID = @EmployeeID OR @EmployeeID IS NULL)

ALTER PROCEDURE [Employee].[usp_getAllEmployeesReliefs]
    @EmployeeID INT = NULL -- default value to make the parameter optional
AS
BEGIN
    SELECT EmployeeReliefID,
           Amount,
           [Employee].[Relief].IsActive,
           PersonalInfo.PersonalInfoName,
           [Setup].Reliefs.Name AS ReliefName
    FROM [Employee].[Relief]
    INNER JOIN PersonalInfo ON PersonalInfo.pk_PersonalInfo_ID = Employee_ID
    INNER JOIN [Setup].Reliefs ON [Setup].Reliefs.ReliefID = [Employee].[Relief].Relief_ID
    WHERE (employeeID = @EmployeeID OR @EmployeeID IS NULL)
    ORDER BY EmployeeReliefID DESC

END

这样,您可以使用或不使用参数调用您的过程。

EXEC usp_getAllEmployeesReliefs --get all records
EXEC usp_getAllEmployeesReliefs 111 --get record with ID 111