sql - Trouble using temp table in where clause -


i'm creating report user can select between 2 statuses of field. default completed status , any. if selected status not completed, needs ignore or select values. created temp table of distinct values , set local variable. i'm trying use case statement , temp table in where clause handle this, it's not letting me. there better way handle other setting sql string , exec'ing it?

declare @paramcompleted varchar(20) = 'completed' declare @paramcommitmentexamined varchar(20) = 'requested' declare @paramdatefrom date declare @paramdateto date  declare @purchstatus table([status] varchar(15)) insert @purchstatus([status]) select distinct tx01stat dimfile_t order tx01stat --select * @purchstatus select      dbo.parsedate(o.orddate) orddate,     t.tx04stat,--commitment examined & type     e.examiner,     t.tx02stat,--outside search     c.county,     s.settstat,     case         when l.loanamt = 0 'cash'         else cast(l.loanamt varchar(10))     end loanamt,     t.tx01stat,--purchase agreement received     m.mktsourc,     case          when c.firmfile '%pc%' 'commercial'         when c.firmfile '%rc%' 'commercial'         else 'residential'     end firmfile,     c.firmfile filenum,     'metes & bounds' lotunit,     s.statcmt     dimfile_o o     left join dimfile_t t on t.firmfile = o.firmfile     left join dimfile_e e on e.firmfile = o.firmfile     left join dimfile_c c on c.firmfile = o.firmfile     left join dimfile_s s on s.firmfile = o.firmfile     left join dimfile_l l on l.firmfile = o.firmfile     left join dimfile_m m on m.firmfile = o.firmfile     left join dimfile_p p on p.firmfile = o.firmfile     t.tx04stat in (@paramcommitmentexamined)     , t.tx01stat in (         case             when @paramcompleted = 'completed' @paramcompleted             else @purchstatus         end) 

you need select. but, think in improved:

where t.tx04stat =  @paramcommitmentexamined ,       ((@paramcompleted = 'completed' , t.tx01stat = @paramcompleted) or        (coalesce(@paramcompleted, '') <> 'completed' , t.tx01stat in (select status @purchstatus))       ) 

don't use in string variables. you'll start think in ('completed,requested') checks 2 values rather one.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -