Tuesday 18 April 2023

Multiple parameters in a select statement and exclude it if it is null

SELECT *

  FROM AdminUnitTemplates aut

  WHERE ID = COALESCE(@ID, ID)

    AND TempName = COALESCE(@TempName, TempName)

    AND Title = COALESCE(@Title, Title)

       AND AdminUnitID = COALESCE(@AdminUnitID, AdminUnitID)


Using the previous query, you can skip using if statements to generate the query.