I have the following SP in SQL:
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)
This is working great with the filters, when @groupuid is empty GUID then it doesn't filter by groupuid, otherwise it filters by groupuid...
Now, what I need is a little more complicated because I have to combine other parameter:
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)
This it not working ... what I need is that if @includedependants = 1 then it will filter emp.specialgroupuid, otherwise it will filter emp.groupuid.
Any clue on how to solve it? I'm sure its something simple :)
Appreciate the help in advance.
AND (
(
@includedependants = 0 AND
(@groupuid = @emptyuid OR emp.groupuid = @groupuid)
)
OR emp.specialgroupuid = @groupuid
)
The OR
part will always run, no matter the value of @includedependants
, which I guess is not what you want. What you want is something more like;
AND (
(
@includedependants = 0 AND
(@groupuid = @emptyuid OR emp.groupuid = @groupuid)
)
OR
(
@includedependants = 1 AND
emp.specialgroupuid = @groupuid
)
)
I would use a case. Unfortunately the required syntax makes it pretty ugly.
AND 1 = case when @includedependants = 1 then
case when emp.specialgroupuid = @groupuid then 1 else 0 end
else
case when @groupuid = @emptyuid OR emp.groupuid = @groupuid then 1 else 0 end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.