3

レコードがOracleにまだ存在しない場合、どうすれば挿入できますか?これが私がやろうとしていることです:

IF NOT EXISTS (SELECT * FROM AROH_FAA_AIRPORT WHERE AIRPORT_ABBREVIATION = '0D7') 
BEGIN 
  insert into AROH_FAA_AIRPORT 
  (FAA_AIRPORT_OID,CITY,FAA_LISTED_AIRPORT_NAME,AIRPORT_ABBREVIATION) 
  values(AROH_FAA_AIRPORT_SEQ.nextval,'ADA','ADA','0D7') 
END 
4

1 に答える 1

7
insert into AROH_FAA_AIRPORT 
   (FAA_AIRPORT_OID,CITY,FAA_LISTED_AIRPORT_NAME,AIRPORT_ABBREVIATION) 
select AROH_FAA_AIRPORT_SEQ.nextval,'ADA','ADA','0D7') 
from dual 
where not exists (select 42
                  from AROH_FAA_AIRPORT 
                  WHERE AIRPORT_ABBREVIATION = '0D7');

または、MERGEステートメントを使用することもできます。

merge into AROH_FAA_AIRPORT a
using (
    select 'ADA' city, 
           'ADA' as faa_listed_airport_name, 
           '0D7' as as airport_abbreviation 
    from dual 
) t ON (t.city = a.city)
when not matched then 
  insert 
     (FAA_AIRPORT_OID,
      CITY,
      FAA_LISTED_AIRPORT_NAME,
      AIRPORT_ABBREVIATION)
  values 
     (AROH_FAA_AIRPORT_SEQ.nextval, 
      t.city, 
      t.faa_listed_airport_name, 
      t.airport_abbreviation);
于 2012-11-27T21:42:38.187 に答える