sql - IN CLAUSE CONTAIN MANY VALUES - TSQL -


i need advice.

i have case statement in t-sql query this:

case      when plan_given in ( list contains 1700 values compare ) 'p1'     when plan_given in ( list contains 1800 values compare ) 'p2'       else null end plan_name 

i cannot list 1700 values in ( ) 'p1' not values.

i tried saving 1700 values , 1800 values lookup_table

lookup_table    columna = 1700 values                  columnb = 1800 values 

and used query:

case      when plan_given in (select columna lookup_table) 'p1'     when plan_given in (select columnb lookup_table) 'p2'       else null end plan_name 

the above code working taking more time execute 10 minutes complete execution.

is there alternative way achieve this?

instead of using plan_given in, try using exists:

case      when exists (select null lookup_table plan_given = columna)           'p1'     when exists (select null lookup_table plan_given = columnb)           'p2'   else null end plan_name 

alternatively, try joining lookup table , comparing lookup values in case expression:

select ...        case plan_given            when lookup_table.columna 'p1'            when lookup_table.columnb 'p2'              else null        end plan_name        ... ... left join lookup_table    on plan_given in (lookup_table.columna, lookup_table.columnb) 

Comments