嘗試基於reference_no有效地檢查記錄是否存在,如果它確實返回了它的ID,如果不插入它並返回一個新的ID。編寫Oracle函數檢查記錄是否存在,如果不將它合併到同一個表中
也許合併是不是要走的路?
找不到語法!
create or replace FUNCTION get_note_history_id(
p_owner_id VARCHAR2, --to insert to the note history table
p_open_date DATE, --to insert to the note history table
p_note_log CLOB, --to insert to the note history table
p_collection_id VARCHAR2, --not to break the previous code
p_reference_no NUMBER --correlates to the notice (date) sent)
)
return VARCHAR2 is
v_note_history_id NUMBER
MERGE INTO NOTE_HISTORY n
USING(
select NOTE_HISTORY_ID
from NOTE_HISTORY
where p_reference_no = n.notice_reference_no
) h
WHEN MATCHED THEN
v_note_history_id := h.NOTE_HISTORY_ID;
WHEN NOT MATCHED THEN
INSERT INTO NOTE_HISTORY (create_user_id,create_date,note,collection_id,notice_reference_no)
VALUES (p_owner_id,p_open_date,p_note_log,p_collection_id,p_reference_no) returning NOTE_HISTORY_ID into v_note_history_id;
RETURN v_note_history_id;
END get_note_history_id;
合併是一個結合了更新和插入的SQL DML語句。不是可以有條件地執行任意PL/SQL語句的PL/SQL控制語句流。返回子句適用於插入,但不適用於合併的插入臂。 –