简体   繁体   中英

Oracle Procedure to Drop a Table as Argument and recreate it

I'm trying to create an oracle procedure transfer_table(table_name) that does the following

  1. drop table_name in schema_a
  2. create table_name in schema_a as (a copy or select * ? ) of table_name@schema_b

I need this procedure to be fail safe: if table_name doesn't exist in schema_a, still go ahead and create it as a copy of table_name@schema_b

This is where I got so far but it doesn't seem to be working well...

create or replace
PROCEDURE transfer_table
  (
      @pTableName  NVARCHAR(128)
  )
  IS

  BEGIN
      execute immediate 
          'DROP TABLE '|| pTableName;
      execute immediate
          'CREATE TABLE '||pTableName|| ' AS SELECT * FROM ' || pTableName || '@SCHEMA_B';

  END;

Would appreciate any help and any optimizations in my way of doing things. The goal really is to copy a table from schema B to schema A, as a procedure

Updated code - still doesn't compile correctly:

create or replace
PROCEDURE transfer_table
  (
      table_name VARCHAR2(30)
  )
  IS
  BEGIN
    BEGIN
      execute immediate 
          'DROP TABLE '|| table_name;    
      WHEN OTHERS THEN
        IF SQLCODE != -942 THEN
          RAISE;
        END IF;
          end;
    END;
        EXECUTE IMMEDIATE
          'CREATE TABLE '||table_name|| ' AS SELECT * FROM ' || table_name || '@SCHEMA_B';

  END;
  /

pTableName || '@SCHEMA_B'

That's incorrect. You need to refer a table in a schema as SCHEMA.TABLE_NAME .

Regarding your script, let's see step by step:

I am user Lalit, I want to create a table EMP as a copy of the same table from schema SCOTT.

SQL> SHOW USER
USER is "LALIT"
SQL> SELECT * FROM lalit.emp;
SELECT * FROM lalit.emp
                    *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> select count(*) from SCOTT.EMP;

  COUNT(*)
----------
        14

So, it confirms EMP table is NOT in user LALIT, it is in SCOTT. I will now create it:

SQL> DECLARE
  2  table_name VARCHAR2(30);
  3  schema_name varchar2(30);
  4  BEGIN
  5  table_name:='EMP';
  6  schema_name:='SCOTT';
  7  begin
  8   EXECUTE IMMEDIATE
  9            'DROP TABLE '|| table_name;
 10            EXCEPTION
 11     WHEN OTHERS THEN
 12        IF SQLCODE != -942 THEN
 13           RAISE;
 14        END IF;
 15            end;
 16        EXECUTE IMMEDIATE
 17            'CREATE TABLE '||table_name|| ' AS SELECT * FROM ' || schema_name||'.'||table_name || '';
 18  END;
 19  /

PL/SQL procedure successfully completed.

Let's confirm:

SQL> select count(*) from lalit.emp;

  COUNT(*)
----------
        14

SQL>

So, I now have the table EMP in schema LALIT.

Your update code has some issues - the parameter arguments don't require length for the the varchar. There's an extra end as well. There's no EXCEPTION keyword for the nested Anonymous block

This is how it should be:

CREATE OR REPLACE PROCEDURE transfer_table(table_name IN VARCHAR2) IS
BEGIN
  BEGIN
    EXECUTE IMMEDIATE 'DROP TABLE ' || table_name;
  EXCEPTION
    WHEN OTHERS THEN
      IF SQLCODE != -942 THEN
        RAISE;
      END IF;
  END;

  EXECUTE IMMEDIATE 'CREATE TABLE ' || table_name || ' AS SELECT * FROM ' || table_name || '@SCHEMA_B';
END transfer_table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM