I want to create a stored procedure to query 'running' tasks. Additionally I want the procedure to have two optional filter parameters to only get running tasks for certain users and task types (airflow ids). Here is what I have so far:
CREATE PROCEDURE [dbo].[spTask_GetRunning]
@UserID NVARCHAR(128) NULL,
@AirflowDagID INT NULL
AS
BEGIN
SELECT ID, UserID, AirflowDagID, [Name], [StartTime], [FinishedTime], [Status], [Parameter]
FROM dbo.[Task]
WHERE [Status] = 'running'
AND (
@UserID IS NULL OR
UserID = @UserID
)
AND (
@AirflowDagID IS NULL OR
AirflowDagID = @AirflowDagID
)
END
This seems to work fine, however since this is the first time I'm in need of optional parameters and posts on the topic seem quite old, I'm wondering if this is the recommended way to go. For example, this post suggest creating different queries for better optimization (though again the post is quite old).