I have a procedure and need to bypass the WHERE clause where input as null.
Please find the below condition.
CREATE PROCEDURE [dbo].[spPERSON_SELECT]
(
@PersonID INT = NULL,
@OrganizationID INT = NULL,
@ManagerID INT = NULL
)
AS
BEGIN
SELECT PERSON.name, ORGANIZATION.name,PERSON.manager
FROM PERSON
INNER JOIN ORGANIZATION
ON PERSON.person_id = ORGANIZATION.person_id
WHERE PERSON.person_id = @PersonID
and ORGANIZATION.organization_id = @OrganizationID
and PERSON.manager_id = @ManagerID
END
GO
Iam facing an issue while the input as null, at that case need to avoid where condition.
Example:
If @PersonID is null then my select query needs the following structure:
SELECT PERSON.name, ORGANIZATION.name,PERSON.manager
FROM PERSON
INNER JOIN ORGANIZATION
ON PERSON.person_id = ORGANIZATION.person_id
WHERE ORGANIZATION.organization_id = @OrganizationID
and PERSON.manager_id = @ManagerID
I would like to avoid dynamic queries.
Bracket each parameter and test for null i.e.
(PERSON.manager_id=@ManagerID or @ManagerID is NUll)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With