|
■No20579 (キキ さん) に返信
> IF DEPT.CODE = CODEA THEN
> UPDATE DEPT SET NAME = NAMEA;
> ELSE IF
> INSERT(CODE,NAME)VALUES(CODEA,NAMEA);
> END IF
> COMMIT;
> こうですかね・・・
PL/SQLの例(1):
BEGIN
INSERT INTO DEPT (CODE,NAME)VALUES(CODEA,NAMEA);
EXCEPTION
WHEN OTHERS THEN
UPDATE DEPT SET NAME=NAMEA WHERE CODE=CODEA;
END;
/
PL/SQLの例(2):
DECLARE
CURSOR curDEPT IS SELECT * FROM DEPT WHERE CODE=CODEA;
recDEPT curDEPT%ROWTYPE;
BEGIN
OPEN curDEPT;
FETCH curDEPT INTO recDEPT;
IF curDEPT%FOUND THEN
UPDATE DEPT SET NAME=NAMEA WHERE CODE=CODEA;
ELSE
INSERT INTO DEPT (CODE,NAME)VALUES(CODEA,NAMEA);
END IF;
CLOSE curDEPT;
END;
>>>># ORACLEにはMERGE INTO... てのがあるそうな
> ありがとうございます。その場合、
MERGE INTO DEPT
USING (
SELECT * FROM DEPT WHERE CODE=CODEA
) DUMMY
ON (DEPT.CODE = DUMMY.CODE)
WHEN MATCHED THEN
UPDATE SET
NAME=DUMMY.NAMEA
WHEN NOT MATCHED THEN
INSERT INTO DEPT (CODE,NAME) VALUES (DUMMY.CODEA,DUMMY.NAMEA);
※コードは全て未検証です。
|