How can i make this If statement to skip to the below if statement? I tried else if,then, but it doesnt work. I want it to display regardless of whether input is passed into the variable.
declare @sql varchar(8000)
declare @sql_where varchar(8000)
declare @ww varchar(10)
declare @plant varchar(10)
declare @plnr_id varchar(10)
set @plant = 'CS58'
set @ww = '201240'
set @plnr_id = 'G37'
set @sql_where = 'where'
set @sql = 'select * from rout_sub_doi '
if len(@plant) > 0
set @sql_where = @sql_where + ' plant = ''' + @plant + ''' '
if len(@ww) > 0
set @sql_where = @sql_where + 'AND sys_intel_ww = ''' + @ww + ''' '
if len(@plnr_id) > 0
set @sql_where = @sql_where + ' AND plnr_id = ''' + @plnr_id + ''' '
print @sql
print @sql_where
exec (@sql + @sql_where )
The common way to handle unknown number of conditions is to make the first one
WHERE 1=1or something benign such asWHERE id IS NOT NULLwhen id is known to be not-nullable.