Best Way write Query for parameter

  • I have parameter clientid if not 'ALL' then i want to join with temp table otherwise i donot want to join with that table i wrote if else but i think there so be good way to write in single query. Any suggestion

    DECLARE @clientid NVRACHAR(20) = '2,3,4'

    IF @clientid <> 'ALL'

    BEGIN

    SELECT [AssigneeID]

    FROM [dbo].Assignee INNER JOIN

    Temp_Client ON Clientid = Client_ID

    ORDER BY [AssigneeID]

    END

    ELSE

    BEGIN

    SELECT [AssigneeID]

    FROM [dbo].Assignee

    ORDER BY [AssigneeID]

    END

  • Not sure which ClientId column is in Temp_Client, so change if needed:

    SELECT [AssigneeID]

    FROM [dbo].Assignee

    LEFT OUTER JOIN Temp_Client ON Clientid = Client_ID

    WHERE @clientid = 'ALL' OR Temp_Client.Clientid IS NOT NULL

    ORDER BY [AssigneeID]

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply