Oracle 将查询语句[insert,update]作为参数传递并执行

Oracle 将查询语句[insert,update]作为参数传递并执行,oracle,oracle10g,oracle11g,Oracle,Oracle10g,Oracle11g,如果我想将insert语句作为参数传递给这个函数,并在函数和返回值中执行它,我该怎么办 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

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

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;
使用“”语句:

这是一个简单的示例,只要执行此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;

使用动态SQL,例如执行即时“插入语句”;