sql server - SQL query error from case when statement with subquery -


my stored procedure:

create procedure test_template  @template_type int=null begin    select        template_id,       template_name               bdc_templates           template_can_be_applied_to in (             case                when @template_type=0 or @template_type=1  @template_type  -- 0:email & letter , 1: email                             when @template_type=2 (select data udf_dp_split_string('0,2', ',')) -- 2: letter               else template_can_be_applied_to                          end)   end 

above stored procedure returns following error:

msg 512, level 16, state 1, procedure test_template, line 6
subquery returned more 1 value. not permitted when subquery follows =, !=, <, <= , >, >= or when subquery used expression.

when executed following inputs:

exec test_template 2 

i using in clause in where condition because when @template_type '2', template_can_be_applied_to column can 1 of values 0 , 2.

first of can't use int variable in statement, need use table variable select statement.

update can’t use scalar variable in in statement record set. if variable has single data value (as think trying do) ok. in case solution provided @ah_hau use equality sign “template_can_be_applied_to = @template_type” better.

secondly can't have multiple records returned in case statement. in order achieve trying need use conditional or , and statements such below:

where (        (@template_type=0 or @template_type=1)          , template_can_be_applied_to in (select col [your table variable])       )  or   (        @template_type=2     , template_can_be_applied_to in (select data udf_dp_split_string('0,2', ','))      ) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -