Oracle實現資料不存在則插入,資料存在則更新(insert or update),oracleinsert

來源:互聯網
上載者:User

Oracle實現資料不存在則插入,資料存在則更新(insert or update),oracleinsert

      思路是寫一個函數,先按條件查詢資料,如果查詢到資料則更新,如果沒有查詢到資料則插入:

create or replace function fn_merge_index(statdate      in date,                                          cpid          in varchar2,                                          indextypecode in number,                                          indexitemcode in number,                                          indexdata     in varchar2)  return number is  numb number;begin  select count(*)    into numb    from cp_index_statistics_rec   where stat_date = to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')     and cp_id = cpid     and index_type_code = indextypecode     and index_item_code = indexitemcode;  if numb = 0 then    --資料不存在,insert    begin      insert into cp_index_statistics_rec        (stat_id,         stat_date,         diagnosis,         cp_id,         is_validate,         index_type_code,         index_item_code,         stat_data,         stat_create_date,         cp_name)      values        (cp_index_statistics_rec_seq.nextval,         to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd'),         '',         cpid,         1,         indextypecode,         indexitemcode,         indexdata,         (select sysdate from dual),         (select cp_name from cp_templet_master where cp_id = cpid));      commit;    end;  else    --資料存在,update    begin      update cp_index_statistics_rec         set is_validate      = 1,             stat_data        = indexdata,             stat_create_date =             (select sysdate from dual)       where stat_date = to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')         and cp_id = cpid         and index_type_code = indextypecode         and index_item_code = indexitemcode;      commit;    end;  end if;  return numb;end fn_merge_index;
注意to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')這個寫法,如果寫成to_date(statdate, 'yyyy/mm/dd'),根據NLS不同,可能導致資料出錯。具體請看這裡


另外oracle提供了merge into可以實現此功能,理論上講比上面的效率會高,但是沒做實驗。merge into有個缺點就是在10g以下版本的oracle中會出現問題,導致比較嚴重的後果(據說會把所有的資料都更新,而9i又不支援在update後加條件),所以我沒有採用這個方法。

merge into的用法:

merge into bonuses d using (select employee_id, salary, department_id from employees where department_id = 80) s on (d.employee_id = s.employee_id) when matched then update set d.bonus = d.bonus + s.salary*.01 when not matched then insert (d.employee_id, d.bonus) values (s.employee_id, s.salary*0.01); 

另外還有個思路,直接update,執行後會返回受影響的行數,如果行數為0,表示沒有合格資料,後面執行insert;如果行數大於0,表示有合格行數且update執行成功。


oracle表中如果存在就不更新,如果不存在就插入的語句怎寫

一條語句寫不出來的
通常思路,先判斷表裡有沒有2011資料,沒有再插入
 
oracle 如果存在主鍵則更新(update),否則添加新紀錄(insert)怎寫

這個容易,用merge就行了,具體用法大致如下、
merge into 目標表
using(select ……) -----這裡是要更新或要插入的資料,一般用select來構造
on ……------這裡是更新判斷時的關聯條件
when match then update 表名 set 欄位名=…… -----如果能匹配(即有資料)則更新
WHEN NOT MATCHED THEN
INSERT (欄位列表)
values(值列表)

大致如此,具體更詳細的用法你可以進一步百度MERGE的用法,資料很多的,這裡不贅述了
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.