I am writing a number of scripts to transform some tables using DBMS_REDEFINITION. Is there an easy way to debug the “ORA-42016: shape of interim table does not match specified column mapping” error?
I am generating the column map dynamically with PL/SQL similar to this:
CREATE TABLE my_table_redef AS SELECT * FROM my_table WHERE 1=0;
ALTER TABLE my_tabl_redef ADD (
column_b INT,
column_id INT NOT NULL
);
DECLARE
col_map VARCHAR2(32767);
CURSOR col_cursor IS
SELECT column_name
FROM user_tab_columns
WHERE table_name = 'MY_TABLE'
ORDER BY column_id;
BEGIN
FOR col IN col_cursor LOOP
col_map := col_map || col.column_name || ' ' || col.column_name || ', ';
END LOOP;
col_map := col_map || 'my_function(column_a) column_b, '
|| 'my_sequence.NEXTVAL column_id';
DBMS_REDEFINITION.START_REDEF_TABLE('user', 'my_table', 'my_table_redef', col_map, DBMS_REDEFINITION.CONS_USE_ROWID);
END;
/
The function my_function
works for other DBMS_REDEFINITION
s, so I know that’s not the problem.
I’ve tried outputting col_map
and doing:
INSERT INTO my_table_redef
SELECT <col_map>
FROM my_table;
with success, so I know that there are no columns wrong.
What else can I try/do to figure out what’s causing the problem?
Edit: In this case, the problem turned out to be typos in the generated column map, which obviously would not be caught by my insert/select test. To that end, I will add a parsing out the destination column names and doing a select from the redef table to my repertoire of debug techniques.