In my sql query I want to use sequence inside CASE statement. The problem is, that I can't use NEXT VALUE FOR inside CASE. I was trying to define user function, but restrictions about using exec and modifying data in UDF made my solution useless...
Here is my test code:
create sequence temp_test start with 1
select
'value1',
'value2',
case when(1=1)
then next value for temp_test
else (select current_value from sys.sequences where name='temp_test')
end
drop sequence temp_test
All I need is increasing variable if condition is true and return it. Any idea?
The exact error is
NEXT VALUE FOR function cannot be used within CASE, CHOOSE, COALESCE, IIF, ISNULL and NULLIF.