본문 바로가기
MS-SQL/Query

[MS-SQL] 여러개 열을 하나의 행으로 출력하는 간단한 예제입니다.

by Hwoarang757 2015. 1. 15.

 열을 하나의 행으로 구분자를 이용해 합치기 간단한 예제

 

 

DECLARE @CITYS NVARCHAR(20)

 

WITH TEST_TABLE AS

(

 

                                        SELECT '서울' AS CITY

                                        UNION ALL

                                        SELECT '인천' AS CITY

                                        UNION ALL

                                        SELECT '경기' AS CITY

                                        UNION ALL

                                        SELECT '충남' AS CITY

                                        UNION ALL

                                        SELECT NULL AS CITY

)

 

 

SELECT @CITYS =  COALESCE(@CITYS+',','')  + COALESCE(CITY,'')  FROM TEST_TABLE

 

 

SELECT @CITYS