我有一個過程,並且需要繞過輸入爲空的WHERE子句。當filter參數爲空時繞過WHERE子句
請找到以下條件。
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面臨的一個問題,而輸入爲空,在這種情況下,需要避免在哪裏條件。
例子:
如果@PersonID爲null,則我的選擇查詢需要的結構如下:
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
我想避免動態查詢。
我喜歡這個回答 – Mehdi