0

如果我想将插入语句作为参数传递给该函数并在函数中执行并返回值,我该怎么办

create or replace function new_record (p_name your_table.first_name%type)
    return your_table.id%type
is
    return_value your_table.id%type;
begin
    begin
        insert into your_table (id, first_name)
            values (your_seq.nextval, p_first_name)
        returning id into return_value;
    exception
        when dup_val_on_index then
             return_value := 0;
    end;
    return return_value;
end;
4

1 回答 1

1

使用“立即执行”语句:

这是一个简单的例子,只要执行这个 sql,你就会看到它是如何工作的:

declare
  p_param   number := 123;
  l_res varchar2(10);
  l_sqltext varchar(100);
begin
  l_sqltext := 'begin select t.dummy into :1 from dual t where 123 = :2; end;';
  execute immediate l_sqltext using out l_res, in p_param;
  dbms_output.put_line(l_res);
end;
于 2013-04-08T10:06:02.050 回答