Copy table from another schema doesn't work dynamically

Viewed 24

I'm trying to copy tables (schema+data) from one schema to another by using: create table as select * from my_table

I want to do it from a certain table list, so I wrote a cursor

DECLARE
   p_site nvarchar2(200);
   v_cmd nvarchar2(1000);
   v_tablename nvarchar2(100);

  CURSOR export_running IS
  SELECT  tablename  FROM TABLES_TABLE;
BEGIN
  p_site:='site_name';
  OPEN export_running;
  LOOP
    FETCH  export_running  INTO v_tablename;
    EXIT WHEN export_running%NOTFOUND;
     
     v_cmd:='create table '||v_tablename||' as select * from '||p_site||'.'||v_tablename;
     execute immediate v_cmd;
  END LOOP;
  CLOSE export_running;
END;

When I run the code I get

PLS-00382: expression is of wrong type
ORA-06550: line 20, column 6:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"

But if I print the statement and run it, it works well.

  • Datapump is not an option here.
  • I'm doing it on SQL Developer, Oracle version 12.1.
  • I have full privileges on both schemas.

Is it a known issue that I cannot dynamically create a table from one schema to the other? Any suggestion?

Thanks!

1 Answers

Don't use NVARCHAR2 data type. From the documentation:

execute_immediate_statement

dynamic_sql_stmt

String literal, string variable, or string expression that represents a SQL statement. Its type must be either CHAR, VARCHAR2, or CLOB.

Related