프로시저를 짜야하는데 어떻게 짜야할 지 감이 안잡히네요..
원래 테이블 구조가 아래와 같고
제품코드 | 제품명 | 수량 | 일련번호 | ||
1 | 핸드폰 | 2 | 10 & 12 $ 13 | ||
2 | 배터리 | 5 | 50 & 75 $ 100 | ||
|
Comment 2
-
Terry
2017.08.07 12:31
-
항해자™
2017.08.08 13:53
스트링 파싱을 해야 하는데, 젤 빠른건 clr 이고, 차선책으로 쓸만한 것은 xml 파싱입니다.
xml 파싱을 사용한다면, 아래와 같은 방식으로 해 볼수 있겠네요,,
create function dbo.fnTbl2Int
( @pString xml
) returns table
as
return (
select row_number() over(order by (select 1)) as cRows
,col.value('@n','int') as cValuefrom @pString.nodes('/x') as a (col)
)
go
declare @vString varchar(max) ='10&12&13'
declare @vXML xml = convert(xml,N'<x n="'+ replace(@vString,'&','"/><x n="') +'"/>')
select * from dbo.fnTbl2Int(@vXML)위 함수로 한번 만들어 봣습니다,,
select *
from dbo.tSample as a
cross apply
dbo.fnTbl2Int(N'<x n="'+ replace(a.cSplits,N'&',N'"/><x n="') + N'"/>') as b
go
;with tblA(제품코드,제품명,수량,일련번호) As
(
Select 1,N'핸드폰',2,'10 & 12 $ 13 ' Union All
Select 2,N'배터리',5,'50 & 75 $ 100 '
)
,tblB(제품코드,제품명,수량,일련번호,남은일련번호,seq) As
(
Select a.제품코드
,a.제품명
,a.수량
,SubString(a.일련번호,1,Charindex('&',a.일련번호)-1) As 일련번호
,SubString(a.일련번호,Charindex('&',a.일련번호)+1,Len(a.일련번호)) As 남은일련번호
,1
From tblA a
Union All
Select a.제품코드
,a.제품명
,a.수량
,SubString(a.남은일련번호,1,Charindex('$',a.남은일련번호)-1) As 일련번호
,SubString(a.남은일련번호,Charindex('$',a.남은일련번호)+1,Len(a.남은일련번호)) As 남은일련번호
,2
From tblB a
Where CharIndex('$',a.남은일련번호) <> 0
And a.seq = 1
Union All
Select a.제품코드
,a.제품명
,a.수량
,a.남은일련번호 As 일련번호
,'' As 남은일련번호
,3
From tblB a
Where CharIndex('$',a.남은일련번호) = 0
And a.seq = 2
)
Select a.제품코드
,a.제품명
,a.수량
,Ltrim(Rtrim(a.일련번호)) As 일련번호
From tblB a
Order By a.제품코드 asc
,a.일련번호 asc