Replace comma separated master data with description in a column

前端 未结 3 1563
自闭症患者
自闭症患者 2021-01-29 07:48

There are 2 SQL Server tables:

Products:

Name               Status Code
------------------------------------
Product 1          1001, 10         


        
3条回答
  •  無奈伤痛
    2021-01-29 08:22

    You can try this. I used a temporary table but you can use any means you want

    SELECT p.Product , p.Status , s.Description INTO #tmp FROM ( SELECT Name Product, TRIM(value ) Status FROM tblProduct CROSS APPLY STRING_SPLIT(TRIM(StatusCode), ',') ) p JOIN tblStatus s ON s.Code = p.Status

    select distinct Product , stuff(( select ',' + tmp2.Description from #tmp tmp2 where tmp2.Product = tmp1.Product order by tmp2.Product for xml path('') ) ,1,1,'') as StatusCode from #tmp tmp1 group by Product

提交回复
热议问题