intA
intA

Reputation: 2681

Conditional WHERE clause to combine two queries

I have a large SELECT INTO statement in a T-SQL script and currently I have two separate SELECT INTO's only differing by one OR condition in the WHERE clause. If my variable @cycle_nbr = 1 I have it doing one SELECT INTO, if @cycle_nbr = 0 I have it doing the other SELECT INTO.

I was wondering if there was a way to do this in one SELECT INTO with the @cylce_nbr condition in the WHERE itself.

Here is my WHERE clause:

WHERE ((a.gl_indicator = '0' OR a.gl_indicator = '1') 
       AND (a.gl_ins_type = '1' OR a.gl_ins_type = '3' ) 
       AND rel_file_nbr is NULL 
       AND a.alpha_line NOT LIKE '%Z'
       AND mis_process_dt >= @start_dt 
       and acctg_cyc_ym  = @acctg_cyc) 
       OR (a.prem_sys_cd='T' AND acctg_cyc_ym  = @acctg_cyc ) 

I only want this last condition OR (a.prem_sys_cd='T' AND acctg_cyc_ym = @acctg_cyc ) in there if @cycle_nbr = 1. Can I put an IF in there somewhere to make this work? Or do I have to stick with the IF(@cycle_nbr = 1) run this select ELSE run the other select?

Upvotes: 0

Views: 56

Answers (1)

AHiggins
AHiggins

Reputation: 7219

Include your variable in the OR statement, i.e.,

OR (a.prem_sys_cd='T' AND acctg_cyc_ym = @acctg_cyc AND @cycle_nbr = 1)

Upvotes: 1

Related Questions