1. dummy 테이블을 이용해서, 데이터 체크 후 Insert / Update

MERGE INTO Employees AS E
USING (SELECT 1 AS employee_id, 'PARK' AS name, 'Developer' AS position from SYSIBM.SYSDUMMY1) 
--USING (SELECT :PARAM1 AS employee_id, :PARAM2 AS name, :PARAM3 AS position from SYSIBM.SYSDUMMY1) 
   AS S ON E.employee_id = S.employee_id
WHEN MATCHED THEN 
    UPDATE SET E.name = S.name, E.position = S.position
WHEN NOT MATCHED THEN 
    INSERT (employee_id, name, position)
    VALUES (S.employee_id, S.name, S.position);

 

- 델파이 FireDac으로 파라미터로 데이터 설정 시 에러 발생

- 쿼리로는 정상 실행

 

2. Values를 이용해서, 데이터 체크 후 Insert / Update

MERGE INTO Employees AS E
USING (VALUES (1, 'PARK', 'Developer')) AS S(employee_id, name, position)
--USING (VALUES (:PARAM1, :PARAM2, :PARAM3)) AS S(employee_id, name, position)
ON E.employee_id = S.employee_id
WHEN MATCHED THEN 
    UPDATE SET E.name = S.name, E.position = S.position
WHEN NOT MATCHED THEN 
    INSERT (employee_id, name, position)
    VALUES (S.employee_id, S.name, S.position);

 

- 델파이 FireDac으로 파라미터로 데이터 설정 시 에러 발생

- 쿼리로는 정상 실행

 

3. 테이블의 데이터 건수로 Insert / Update

MERGE INTO Employees AS E
USING (SELECT COUNT(*) AS CNT FROM Employees WHERE employee_id = '1') 
--USING (SELECT COUNT(*) AS CNT FROM Employees WHERE employee_id = :PARAM1) 
   AS S
   ON S.CNT > 0
WHEN MATCHED THEN 
    UPDATE SET E.name = 'PARK', E.position = 'Developer'
--    UPDATE SET E.name = :PARAM2, E.position = :PARAM3  
WHEN NOT MATCHED THEN 
    INSERT (employee_id, name, position)
    VALUES (1, 'PARK', 'Developer');
--    VALUES (:PARAM1, :PARAM2, :PARAM3);

 

- 델파이 FireDac으로 파라미터로 호출해도 정상처리~

 

※ 사용환경에 맞게 쓰면 될 듯!

 

- 출처 : 우리의 친구 ChatGPT 등등

+ Recent posts