In Oracle I want to export tables using dbms_datapump. I have a table containing a list of table names that I want to export a subset of, where the grp = 'batch1'. I want to loop through each of those table names and pass the table name over to the metadata_filter. However it's not working. I specifically want to do this in a loop.
Here are some extracts from the code:
CURSOR lib_cur IS
SELECT table_name
FROM my_table_list
WHERE grp = 'batch1';
Later on in the code:
for x in lib_cur
LOOP
table_nm := x.table_name;
dbms_datapump.metadata_filter
(handle => my_job_handle,
name => 'NAME_EXPR',
value => 'IN (''' || table_nm || ''')' ,
object_type => null);
Does anyone know why this isn't passing the current value of table_nm over to the value parameter of dbms_datapump.metadata_filter? If I hardcode one of the actual table names in that I want e.g. ''table1'' it works fine.
I know that my loop is bringing the correct table names into the table_nm variable as I've output them to dbms_output and can see that with each iteration of the loop comes a different table name.
So any ideas why the name isn't being passed across in my Loop? I don't know if the syntax is incorrect or if it's just not possible to do it this way? The thing is I'm sure I had it working a while ago!
You are replacing the filter each time in your loop. You want to provide the entire list at once. Try using LISTAGG
to assemble a delimited list of tables into a single scalar variable that you can pass to the filter:
SELECT 'IN ('''||LISTAGG(table_name,''',''') WITHIN GROUP (ORDER BY 1)||''')'
INTO var_table_list
FROM my_table_list
WHERE grp = 'batch1';
dbms_datapump.metadata_filter
(handle => my_job_handle,
name => 'NAME_EXPR',
value => var_table_list,
object_type => null);