sql - Removing part of where clause depending on input -


i have following query in want omit filter created_by = @searchby if @searchby's input 1. how can this?

create proc [dbo].[rptcashcollectioninner]     @branchid int,      @searchby int,      @paiddate datetime  begin     select id     reading     created_by = @searchby        , branch_id = @branchid end; 

you can short-circuit search element or operator:

create proc [dbo].[rptcashcollectioninner]   @branchid int, @searchby int, @paiddate datetime begin   select id, reading   (@searchby = 1 or created_by=@searchby) , branch_id=@branchid end; 

or, more elegantly, in operator:

create proc [dbo].[rptcashcollectioninner]   @branchid int, @searchby int, @paiddate datetime begin   select id, reading   @searchby in (1, created_by) , branch_id=@branchid end; 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -