select - SQL Server : case without a null return -
i have following sql server code:
declare @n_count_pr int, @n_count_err int, @v_out_pr varchar(100); begin set @n_count_pr = (select count (*) table_pr) set @n_count_err = (select count (*) table_err) if (@n_count_err > 0 , @n_count_pr > 0) set @v_out_pr = 'output 1' else if (@n_count_err > 0 , @n_count_pr = 0) set @v_out_pr = 'output 2' print @v_out_pr end;
it works fine, need different structure.
i need simple select same thing.
can me?
in procedure above have only 2 outputs nothing else. if there other output other output 1 , output 2 should discarded.
it's like:
if = output 1 else if = b output 2
but don't have else, every other option i=c or = d, don't care. want discard options.
if did want other options this:
if (@n_count_err > 0 , @n_count_pr > 0) set @v_out_pr = 'output 1' else if (@n_count_err > 0 , @n_count_pr = 0) set @v_out_pr = 'output 2' else set @v_out_pr = 'any other option'
--found solution :)
select bit (select case when (select count (*) table_err) > 0 , (select count (*) table_pr) > 0 'output 1' when (select count (*) table_err) > 0 , (select count (*) table_pr) = 0 'output 2' else null end bit) mytable bit not null
try :
select case when (select count (*) table_err) > 0 , (select count (*) table_pr) > 0 'output 1' when (select count (*) table_err) > 0 , (select count (*) table_pr) = 0 'output 2' else '' end
Comments
Post a Comment