2013-03-20 93 views
1

下面的函數編譯得很好。但是當以匿名的PL/SQL代碼調用函數時,調用失敗。 任何人都可以請建議爲什麼?我試圖插入未匹配(新)數據並更新匹配(現有)數據。調用失敗的函數:ORA-00904:「TEMP_DATA」。「P_LNAME」:無效標識符

Function Fn_Insert_Data_Using_Merge (p_Id in Number, 
            p_fname in varchar2, 
            p_mname in varchar2, 
            p_lname in varchar2, 
            p_birth_date in date) Return Boolean 
Is 
Begin 
    Dbms_Output.put_line ('Inside the function Fn_Insert_Data_Using_Merge ...'); 
    Merge Into test_employee te 
    using (select distinct p_Id, p_fname, p_mname, p_lname, p_birth_date 
      from test_employee) temp_data 
    on (te.first_name = temp_data.p_fname and 
     te.middle_name = temp_data.p_mname and 
     te.last_name = temp_data.p_lname) 
    when matched then 
     update 
     set first_name = p_fname, 
      middle_name = p_mname, 
      last_name = p_lname, 
      run_status = 'Updated' 
    when not matched then 
     insert (id, first_name, middle_name, last_name, birth_date, run_status) 
     values 
      (p_id, p_fname, p_mname, p_lname, p_birth_date, 'Inserted'); 

    Dbms_Output.put_line ('Returning successfully from the function Fn_Insert_Data_Using_Merge ...'); 
    Return True; 
Exception 
    When Dup_Val_On_Index Then 
    Dbms_Output.put_line ('The name already exists...Cannot insert again ..'); 
    Return False; 
    When Others Then 
    Dbms_Output.put_line ('Facing some critical error : ' || SQLERRM); 
    Return False; 
End Fn_Insert_Data_Using_Merge; 
+0

錯誤的行號是什麼? – Rachcha 2013-03-20 05:16:04

+0

(te.first_name = temp_data.p_fname和te.middle_name = temp_data.p_mname和te.last_name = temp_data.p_lname) – 2013-03-20 05:41:27

+0

ORA-06512:在「FC113EXT.PKG_TEST」,第101行 ORA-00904:「TEMP_DATA」。「 P_LNAME「:無效標識符 第101行是」合併到test_employee te「語句.. – 2013-03-20 05:49:59

回答

0

該錯誤試圖告訴您TEMP_DATA摘錄中沒有列名爲P_LNAME。名爲'p_lname'的函數有一個參數,但是在提取中,您沒有分配列別名,因此沒有命名字段。另外(儘管不是錯誤的原因),從可能有多行的表中執行DISTINCT是將函數參數獲取到USING子句中的臨時表中的一種較慢的方式;你最好使用SELECT ... FROM DUAL。嘗試用以下方法替換您的MERGE聲明並讓我們知道它是如何發生的:

Merge Into test_employee te 
using (select p_Id as p_Id, 
       p_fname as p_fname, 
       p_mname as p_mname, 
       p_lname as p_lname, 
       p_birth_date as p_birth_date 
     from dual) temp_data 
on (te.first_name = temp_data.p_fname and 
    te.middle_name = temp_data.p_mname and 
    te.last_name = temp_data.p_lname) 
when matched then 
    update 
    set first_name = temp_data.p_fname, 
     middle_name = temp_data.p_mname, 
     last_name = temp_data.p_lname, 
     run_status = 'Updated' 
when not matched then 
    insert (id,    first_name,    middle_name, 
      last_name,   birth_date,    run_status) 
    values 
     (temp_data.p_id, temp_data.p_fname,  temp_data.p_mname, 
      temp_data.p_lname, temp_data.p_birth_date, 'Inserted'); 

分享和享受。

+0

感謝鮑勃。代碼只需進行一次更改即可正常運行。當我使用更新後的代碼時,我收到異常情況,說「ON CLAUSE中提到的列無法更新。」當我刪除這些列時,代碼按照預期的功能工作。 – 2013-03-20 12:32:52