您可以使用這裏的一些方法Turning a Comma Separated string into individual rows來獲取每組值的表,然後將這些表交叉連接在一起並連接值。
我扔在一起,這個例子從上面的鏈接建模關閉CTE答案:
declare @Types as varchar(max)
declare @Areas as varchar(max)
set @Types = 'DP,DPC,PC'
set @Areas = 'M1,M2,M3'
;with tmp(DataItem, Data) as (
select LEFT(@Types, CHARINDEX(',',@Types+',')-1),
STUFF(@Types, 1, CHARINDEX(',',@Types+','), '')
union all
select LEFT(Data, CHARINDEX(',',Data+',')-1),
STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from tmp
where Data > ''
)
select
DataItem
into #Types
from tmp
;with tmp2(DataItem, Data) as (
select LEFT(@Areas, CHARINDEX(',',@Areas+',')-1),
STUFF(@Areas, 1, CHARINDEX(',',@Areas+','), '')
union all
select LEFT(Data, CHARINDEX(',',Data+',')-1),
STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from tmp2
where Data > ''
)
select
DataItem
into #Areas
from tmp2
select
_Types.DataItem + '_' + _Areas.DataItem
from #Types _Types
cross join #Areas _Areas
drop table #Types
drop table #Areas
如果您需要重新加入這些你能看到這篇文章here多個實例,並挑選最適合你的一個。
謝謝,我會給它一個鏡頭!現在像一位母親一樣多任務處理,所以可能會有一段時間。 – Jimfletcha