1

以下函数编译得很好。但是当调用匿名 PL/SQL 代码中的函数时,调用会失败。谁能建议为什么?我正在尝试插入不匹配的(新)数据并更新匹配的(现有)数据。

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;
4

1 回答 1

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');

分享和享受。

于 2013-03-20T10:52:07.963 回答