you need to change the way you are calling the next value. Just remove the select and request the next value. Like below. select case when b_id is ...
stackoverflow.com