我在 SQL 中有以下 SP:
ALTER PROCEDURE [dbo].[search_employee_special]
(
@employeruid uniqueidentifier,
@groupuid uniqueidentifier,
@includedependants bit = 0
)
AS
DECLARE @emptyuid uniqueidentifier
SET @emptyuid = CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER)
SELECT
emp.id,
emp.name
FROM employee emp
WHERE emp.isactive = 1
AND employeruid = @employeruid
AND (@groupuid = @emptyuid OR emp.gropuid = @groupuid)
这对过滤器非常有用,当@groupuid 为空 GUID 时,它不会按 groupuid 过滤,否则它会按 groupuid 过滤...
现在,我需要的有点复杂,因为我必须结合其他参数:
ALTER PROCEDURE [dbo].[search_employee_special]
(
@employeruid uniqueidentifier,
@groupuid uniqueidentifier,
@includedependants bit = 0
)
AS
DECLARE @emptyuid uniqueidentifier
SET @emptyuid = CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER)
SELECT
emp.id,
emp.name
FROM employee emp
WHERE emp.isactive = 1
AND employeruid = @employeruid
--AND (@groupuid = @emptyuid OR emp.gropuid = @groupuid)
AND (
(@includedependants = 0 AND
(@groupuid = @emptyuid
OR emp.groupuid = @groupuid))
OR emp.specialgroupuid = @groupuid)
这不起作用......我需要的是,如果@includedependants = 1,那么它将过滤emp.specialgroupuid,否则它将过滤emp.groupuid。
关于如何解决它的任何线索?我敢肯定它很简单:)
提前感谢帮助。