如何使用绑定变量使整个 PL/SQL 代码块动态化?

How can I make an entire PL/SQL code block dynamic with bind variables?

Background

I'm trying to make a re-usable PL/SQL procedure to move data from one database to another.

For this purpose, I'm using dynamic SQL.

The procedure executes perfectly if I use a REPLACE with placeholders. However, for security reasons, I want to use bind variables.


Question

How can I make an entire PL/SQL code block dynamic (with bind variables)? If I use a REPLACE instead of the bind variables, it works fine.


How to replicate

To replicate this in your database, create the following procedure as it is:

create or replace procedure move_data(i_schema_name in varchar2, i_table_name in varchar2, i_destination in varchar2) as
l_sql varchar2(32767);
l_cursor_limit pls_integer := 500;
l_values_list varchar2(32767);

begin

select listagg('l_to_be_moved(i).' || column_name, ', ') within group (order by column_id)
into l_values_list
from all_tab_cols
where owner = i_schema_name and
      table_name = i_table_name and
      virtual_column = 'NO';

l_sql := q'[
declare
l_cur_limit pls_integer := :l_cursor_limit;

cursor c_get_to_be_moved is
select :i_table_name.*, :i_table_name.rowid
from :i_table_name;

type tab_to_be_moved is table of c_get_to_be_moved%rowtype;

l_to_be_moved tab_to_be_moved;

begin  

open c_get_to_be_moved;
loop
    fetch c_get_to_be_moved
    bulk collect into l_to_be_moved limit l_cur_limit;
    exit when l_to_be_moved.count = 0;      

    for i in 1.. l_to_be_moved.count loop
        begin
            insert into :i_table_name@:i_destination values (:l_values_list);
        exception
        when others then
            dbms_output.put_line(sqlerrm);
            l_to_be_moved.delete(i);
        end;    
    end loop;
    forall i in 1.. l_to_be_moved.count
    delete
    from :i_table_name
    where rowid = l_to_be_moved(i).rowid;    

    for i in 1..l_to_be_moved.count loop
        if (sql%bulk_rowcount(i) = 0) then
            raise_application_error(-20001, 'Could not find ROWID to delete. Rolling back...');           
        end if;
    end loop;    
    commit;
end loop;          
close c_get_to_be_moved;

exception
when others then
    rollback;
    dbms_output.put_line(sqlerrm);
end;]';
execute immediate l_sql using l_cursor_limit, i_table_name, i_destination, l_values_list;
exception
when others then
    rollback;
    dbms_output.put_line(sqlerrm);
end;
/

然后您可以执行以下程序:

begin
    move_data('MySchemaName', 'MyTableName', 'MyDatabaseLinkName');
end;
/

由于多种原因(无法生成适当的执行计划、安全检查等)Oracle 不允许标识符绑定(table 名称、模式名称、列名称等)。因此,如果确实有必要,唯一的方法是在某种验证后对这些标识符进行硬编码(以防止 SQL 注入)。

如果我理解得很好,你可以尝试一个技巧,在动态 SQL 中使用动态 SQL。

设置:

create table tab100 as select level l from dual connect by level <= 100;
create table tab200 as select level l from dual connect by level <= 200;
create table tabDest as select * from tab100 where 1 = 2;

这行不通:

create or replace procedure testBind (pTableName in varchar2) is
    vSQL varchar2(32000);
begin
    vSQL := 'insert into tabDest select * from :tableName';
    execute immediate vSQL using pTableName;
end;

但这样做就可以了:

create or replace procedure testBind2 (pTableName in varchar2) is
    vSQL varchar2(32000);
begin
    vSQL := q'[declare
                vTab  varchar2(30)    := :tableName;
                vSQL2 varchar2(32000) := 'insert into tabDest select * from ' || vTab;
               begin
                 execute immediate vSQL2;
               end;
              ]';
    execute immediate vSQL using pTableName;
end;

我觉得你可以做得更简单。

create or replace procedure move_data(i_schema_name in varchar2, i_table_name in varchar2, i_destination in varchar2) as
l_sql varchar2(32767);

begin



select listagg('l_to_be_moved(i).' || column_name, ', ') within group (order by column_id)
into l_values_list
from all_tab_cols
where owner = i_schema_name and
      table_name = i_table_name and
      virtual_column = 'NO';

l_sql := 'insert into '||i_destination||'.'||i_table_name||' select * from '||i_schema_name||'.'||i_table_name;

execute immediate l_sql;

end;

如果您担心 SQL-注入,请查看包 DBMS_ASSERT。此 PL/SQL 包提供了验证输入值属性的功能。