I try the instance query as below to convert a commas separated list into multiple records and I found the cte table 'tmp' can run on its own until all sub-strings were loaded in a new record. This example query is from this thread.
I don't want to focus on this functionality but the use of the temporary table here. Can anyone please advise what term is used in T-SQL to describe this computing techniques for the cte table? The cte table is recalled by itself in the definition statements and without explicit looping statements the table actually run iteratively until the filter statement where Data > ''
return NULL.
create table Testdata(SomeID int, OtherID int, Data varchar(max))
insert Testdata select 1, 9, '18,20,22,25,26'
insert Testdata select 2, 8, '17,19'
insert Testdata select 3, 7, '13,19,20'
insert Testdata select 4, 6, ''
;with tmp(SomeID, OtherID, DataItem, Data) as (
select SomeID, OtherID, LEFT(Data, CHARINDEX(',',Data+',')-1),
STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from Testdata
union all
select SomeID, OtherID, LEFT(Data, CHARINDEX(',',Data+',')-1),
STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from tmp
where Data > ''
)
select SomeID, OtherID, DataItem
from tmp
order by SomeID