I am using a stored procedure to generate a report based on parameters to SP. I have to join different where conditions depending upon parameters passed.
For ex.
ALTER PROCEDURE [dbo].[sp_Report_InventoryAging]
@TitleFlag int=0, /*0-All veh, 1-Clear Title, 2-Without Clear Title*/
@CompName varchar(100) = 'ALL COMPANIES',
@CompBranchId varchar(50) = 'ALL', /*All Offices*/
@StateId varchar(50)='All States' /*All states*/
Select .... Where TitleFlag=@TitleFlag and
Now I want to specify conditions based on parameters like -
- If not 'ALL COMPANIES' then upper(Company)=upper(@CompName)
- If not 'ALL OFFICES' then OfficeID=@CompBranchId
- If not 'ALL States' then StateID=@StateID
How do I merge all of these conditions within where condition of select statement depending upon parameter value?
Any help is highly appreciated.