꽤나 오래전부터 검색해봐도 적절한 결과를 얻지 못했었는데
오늘에야 발견... 뒷북일 가능성도 농후합니다만, 포스팅해봅니다


먼저 원문 주소 나갑니다

http://weblogs.sqlteam.com/brettk/archive/2005/02/02/4095.aspx

가장 생각하기 쉽고, 또 제가 얼마전까지 사용해왔던 while 문에 대해
Terrific looping technique! 이라고 지껄여 주는 저 센스...


다음은 제가 필요에 의해 간소화한 응용편 소스 나갑니다

--pivot table은 미리 생성하고 필요한만큼 로우 생성
create table pivot(
seq int not null primary key
)

declare @seq int
select @seq = 1
while @seq <= 100
begin
insert into pivot values(@seq)
select @seq = @seq + 1
end
go

declare @str varchar(1000), @delim varchar(10)
select @str = '1,2,3,4,5,6,7,8,9,0', @delim = ','

select substring(@str, pos1+1, case pos2 when 0 then len(@str) - pos1 else pos2 - pos1 - 1 end) as token
from
(
select pos1,charindex(@delim,@str,pos1+1) pos2 from
(
select charindex(@delim,@str,seq) pos1
from pivot
where seq <= len(@str)
) d1 group by pos1
) d2
order by pos1
go

뭐, 굳이 실행안해보셔도 결과는 예상하시다시피 다음과 같습니다

token
------------------------------------------------
1
2
3
4
5
6
7
8
9
0

(10 row(s) affected)
Posted by trust
,