POSTGRES 에서는 ON CONFLICT DO UPDATE 절로 사용하지만
MSSQL 은 MERGE 구문을 이용한다.
postgres 의 upsert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert into CUSTOMERS (NAME, EMAIL) | |
values ( | |
'Microsoft', | |
'hotline@microsoft.com' | |
) on conflict (NAME) | |
do update set EMAIL = EXCLUDED.EMAIL || ';' || CUSTOMERS.EMAIL; |
mssql 의 merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MERGE dbo.CUSTOMERS AS A | |
USING (SELECT 'Microsoft', 'hotline@microsoft.com') AS B (NAME, EMAIL) | |
ON A.NAME = B.NAME | |
WHEN MATCHED THEN | |
UPDATE SET EMAIL = A.EMAIL + ';' + B.EMAIL | |
WHEN NOT MATCHED BY TARGET THEN | |
INSERT (NAME, EMAIL) VALUES (B.NAME, B.EMAIL); |
동시성 처리시 SELECT , UPDATE, INSERT, DELETE 를 한개의 구문에서 처리할 수 있는 유용한 쿼리 구문이다.
NOT MATCHED 에서 UPDATE, DELETE 를 수행하고자한다면 WHEN NOT MATCHED BY SOURCE THEN 으로 수정해 작업한다.
'SQL' 카테고리의 다른 글
[MSSQL] 미사용 인덱스 검색 쿼리 (270) | 2020.09.21 |
---|---|
[MSSQL] 캐시된 실행계획에서 누락된 인덱스 정보를 가지고 있는 개체 찾기 (801) | 2020.09.21 |
[MSSQL] 누락된 인덱스 찾기 (486) | 2020.09.21 |
SQL 변수 선언 및 사용 (2) | 2016.05.19 |
GROUP BY 및 HAVING 그리고 집계 함수 (2) | 2016.05.19 |
댓글