What this query does to create comma delimited list SQL Server?

前端 未结 4 1535
轮回少年
轮回少年 2020-11-22 11:39

I\'ve written this query with the help of google to create a delimited list from a table but I didn\'t understand anything from this query.

Can anyone explain me wha

4条回答
  •  自闭症患者
    2020-11-22 12:06

    Take it apart step by step - from the inside out.

    Step 1:

    Run the innermost query and see what it produces:

    SELECT E2.ename AS 'data()' 
    FROM emp AS e2 
    WHERE e2.DEPTNO = 10
    FOR XML PATH('')
    

    You should get an output something like:

    CLARK KING MILLER
    

    Step 2:

    The REPLACE just replaces spaces with , - thus turning your output into

    CLARK, KING, MILLER
    

    Step 3:

    The outer query gets the deptno value - plus the results from the inner query - and produces your final result.

提交回复
热议问题