I want to generate some records in a recursive CTE using the new choose function
;With Cte As
(
Select
Id=1
To = Cast ('India' as varchar(10))
Union All
Select
Id +1
,To= Cast( Choose(ID,'India','Belgium') as varchar(10))
From Cte
Where Id < 10
)
Select * from Cte
Expected output
Id PlayerName BelongTo
1 Player1 India
2 Player2 Belgium
How can I do so using the Choose function?
CHOOSE is documented to return NULL:
So you need to perform a modulo operation. The modulo operator in SQL is %, so it would be:
(I initially had a -1 from
ID, but given that this statement is executing in a context where theIDvalue is from the previous row, that’s already dealt with)