MSSQL - STUFF / MERGE INTO

์„ฑ์—ฐ์ฃผยท2022๋…„ 1์›” 28์ผ
0

1. STUFF

STUFF("๋ฌธ์ž์—ด", "์‹œ์ž‘์œ„์น˜", "๋ฌธ์ž๊ธธ์ด", "์น˜ํ™˜๋ฌธ์ž")

์˜ˆ์‹œ)

SELECT a.job
     , STUFF((SELECT ',' + ename
                FROM emp
               WHERE job = a.job
                 FOR XML PATH('')
	   ), 1, 1, '') AS enames
  FROM emp AS a
 GROUP BY a.job

๊ฒฐ๊ณผ ๊ฐ’

๐Ÿ‘‰ ๋ฌธ์ž์—ด(enames)์ด ,(๊ตฌ๋ถ„์ž)๋กœ ๊ตฌ๋ถ„๋˜์–ด ํ•œ์ค„์˜ ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅ๋˜๊ณ  ์žˆ์Œ

  • FOR XML PATH('') : ๋ฌธ์ž์—ด๋กœ ์น˜ํ™˜ํ•˜๊ฒ ๋‹ค
  • , 1, 1, '') : ๋งจ์ฒ˜์Œ(1 = ์ธ๋ฑ์Šค)์— ๋‚˜์˜ค๋Š” ๋ฌธ์ž ํ•œ๊ฐœ(1)๋ฅผ ๊ณต๋ฐฑ('')์œผ๋กœ ์น˜ํ™˜ํ•˜๊ฒ ๋‹ค
    // ์›๋ž˜๋Š” ,SCOTT,FORD ์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š”๊ฒƒ์ด ๋งž์Œ

๐ŸŒฑ์ถœ์ฒ˜ : https://gent.tistory.com/344

2. MERGE INTO

MERGE INTO [๋Œ€์ƒ(Target)ํ…Œ์ด๋ธ”] 
USING [์†Œ์Šค(Source) ํ…Œ์ด๋ธ” | ์„œ๋ธŒ ์ฟผ๋ฆฌ] 
ON [์กฐ๊ฑด๋ฌธ] 
WHEN MATCHED THEN 
	[์กฐ๊ฑด ์ผ์น˜ํ•  ๋•Œ์˜ ์ฟผ๋ฆฌ] 	-- ๋‹จ, update/insert/delete ๋งŒ ๊ฐ€๋Šฅ
WHEN NOT MATCHED THEN 
	[์กฐ๊ฑด ๋ถˆ์ผ์น˜ํ•  ๋•Œ์˜ ์ฟผ๋ฆฌ] 	-- ๋‹จ, update/insert/delete ๋งŒ ๊ฐ€๋Šฅ

Merge/Using์ ˆ

  • Target ํ…Œ์ด๋ธ”๊ณผ Source ํ…Œ์ด๋ธ” ๋ชจ๋‘ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ๐Ÿ‘‰ MATCHED ์กฐ๊ฑด์— ํ•ด๋‹น
    // ํ•ด๋‹น ํ–‰์€ Target ํ…Œ์ด๋ธ”์˜ ํ–‰์„ Updateํ•˜๊ฑฐ๋‚˜ Delete ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Target ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ Source ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๐Ÿ‘‰ NOT MATCHED BY SOURCE ์กฐ๊ฑด์— ํ•ด๋‹น
    // ํ•ด๋‹น ํ–‰์€ MATCHED์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Target ํ…Œ์ด๋ธ”์˜ ํ–‰์„ Updateํ•˜๊ฑฐ๋‚˜ Delete ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Source ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ Target ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๐Ÿ‘‰ NOT MATCHED BY TARGET ์กฐ๊ฑด์— ํ•ด๋‹น
    // ํ•ด๋‹น ํ–‰์€ Target ํ…Œ์ด๋ธ”๋กœ INSERT๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

ON์ ˆ

๋Œ€์ƒ ํ…Œ์ด๋ธ”๊ณผ ์†Œ์Šค ํ…Œ์ด๋ธ”์„ ๋น„๊ต(์กฐ์ธ)ํ•  ์กฐ๊ฑด์„ ์ง€์ •ํ•œ๋‹ค. ex) id

ON ์ ˆ์— ์ถ”๊ฐ€ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์„ ๋„ฃ์–ด ๋Œ€์ƒ ํ…Œ์ด๋ธ”์˜ ํ–‰์„ ํ•„ํ„ฐ๋งํ•˜์—ฌ ์ฟผ๋ฆฌ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์˜ˆ๊ธฐ์น˜ ์•Š์€ ์ž˜๋ชป๋œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋  ์ˆ˜ ์žˆ๋‹ค.

WHEN์ ˆ

MATCHED

  • MERGE๋ฌธ์— 1๊ฐœ๋งŒ ํฌํ•จ๋  ์ˆ˜ ์žˆ๋‹ค.
  • MATCHED์ ˆ์„ 2๊ฐœ ํฌํ•จ์‹œํ‚ฌ ๊ฒฝ์šฐ, ์ถ”๊ฐ€ ์กฐ๊ฑด(AND condition)์ ˆ์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ  ๊ฐ๊ฐ UPDATE / DELETE ๋™์ž‘์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

NOT MATCHED [BY TARGET]

  • MERGE๋ฌธ์— 1๊ฐœ๋งŒ ํฌํ•จ๋  ์ˆ˜ ์žˆ๋‹ค.
  • BY TARGET์€ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ON์ ˆ์— ์ผ์น˜ํ•˜์ง€ ์•Š์ง€๋งŒ ์ถ”๊ฐ€ ๊ฒ€์ƒ‰ ์กฐ๊ฑด(์žˆ์„ ๊ฒฝ์šฐ)์„ ์ถฉ์กฑํ•˜๋Š” ๋ชจ๋“  ํ–‰์— ๋Œ€ํ•ด INSERT ์ˆ˜ํ–‰ํ•œ๋‹ค.

NOT MATCHED BY SOURCE

  • MERGE๋ฌธ์— ์ตœ๋Œ€ 2๊ฐœ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
  • WHEN MATCHED๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ ์ ์šฉ๋œ๋‹ค.
  • 2๊ฐœ ํฌํ•จ์‹œํ‚ฌ ๊ฒฝ์šฐ, ์ถ”๊ฐ€ ์กฐ๊ฑด์ ˆ์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ  ๊ฐ๊ฐ UPDATE / DELETE ๋™์ž‘์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
  • ์ถ”๊ฐ€ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์€ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์˜ ์—ด๋งŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
  • MERGE๋Š” ๋™์ผํ•œ ํ–‰์„ ์—ฌ๋Ÿฌ ๋ฒˆ Updateํ•˜๊ฑฐ๋‚˜ Deleteํ•  ์ˆ˜ ์—†๋‹ค.

๐ŸŒฑ์ถœ์ฒ˜ : https://s2choco.tistory.com/35

์˜ˆ์‹œ1) ์ถ”๊ฐ€์ ์ธ ์กฐ๊ฑด์ด ์—†๋Š” ๊ฒฝ์šฐ

MERGE INTO MA_COLOR AS C 
USING (SELECT 1 AS DUAL) AS D -- C์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ๋„ฃ์€ Dummy ๊ฐ’
ON (C.COLORNO = 10) 
WHEN MATCHED THEN  -- ์œ„์˜ ๊ฒฐ๊ณผ๊ฐ€ True
	UPDATE SET C.COLORNM = '๋นจ๊ฐ•', C.FLAG1 = '1' 
WHEN NOT MATCHED THEN -- ์œ„์˜ ๊ฒฐ๊ณผ๊ฐ€ False
	INSERT(COLORNO, COLORNM, FLAG1) VALUESE(10, '๋นจ๊ฐ•', '1') ;

์˜ˆ์‹œ2) ์ถ”๊ฐ€์ ์ธ ์กฐ๊ฑด์ด ์žˆ๋Š” ๊ฒฝ์šฐ

MERGE #TEMP_A AS A -- GUBUN, COLORNO ์ปฌ๋Ÿผ ๊ธฐ์ค€ ๋น„๊ต 

USING (SELECT STATUS, GUBUN, COLORNO, COLORNM, PRICE FROM #TEMP_B) AS B 
ON (A.GUBUN = B.GUBUN AND A.COLORNO = B.COLORNO) 

-- 1. GUBUN ์ปฌ๋Ÿผ, COLORNO ์ปฌ๋Ÿผ ๊ฐ’์ด ์ผ์น˜ํ•œ ํ–‰์ด ์กด์žฌํ•˜์ง€ ์•Š๊ณ , ํ•ด๋‹น ํ–‰์˜ STATUS ์ปฌ๋Ÿผ์˜ ๊ฐ’์ด '์ •์ƒ'์ผ ๊ฒฝ์šฐ 
WHEN NOT MATCHED AND B.STATUS = '์ •์ƒ' THEN 
	INSERT (GUBUN, COLORNO, COLORNM, PRICE) VALUES (B.GUBUN, B.COLORNO, B.COLORNM, B.PRICE) 

-- 2. GUBUN ์ปฌ๋Ÿผ, COLORNO ์ปฌ๋Ÿผ ๊ฐ’์ด ์ผ์น˜ํ•œ ํ–‰์ด ์กด์žฌํ•˜๊ณ , ํ•ด๋‹น ํ–‰์˜ STATUS ์ปฌ๋Ÿผ์˜ ๊ฐ’์ด 'ํ๊ธฐ'์ผ ๊ฒฝ์šฐ 
WHEN MATCHED AND B.STATUS = 'ํ๊ธฐ' THEN 
	UPDATE SET GUBUN = ('ํ๊ธฐํ•œ ' + B.GUBUN) 

-- 3. GUBUN ์ปฌ๋Ÿผ, COLORNO์ปฌ๋Ÿผ ๊ฐ’์ด ์ผ์น˜ํ•œ ํ–‰์ด ์กด์žฌํ•˜๊ณ , ํ•ด๋‹น ํ–‰์˜ STATUS ์ปฌ๋Ÿผ์˜ ๊ฐ’์ด '๋ฐ˜ํ’ˆ'์ผ ๊ฒฝ์šฐ 
WHEN MATCHED AND B.STAUTS = '๋ฐ˜ํ’ˆ' THEN 
	DELETE;

๐ŸŒฑ์ถœ์ฒ˜ : https://woogie-db.tistory.com/40

0๊ฐœ์˜ ๋Œ“๊ธ€