Rename the name of the table dynamically in a loop

Hi all

It might not be the best solution, but it is a starting point for me. I have tables: x1_old, x2_old... x1_new, x2_new...
When I receive new tables, it's x 1, x 2... You need 3 things:
1 x1_old, x2_old must be removed
2 x1_new, x2_new must be renamed to x1_old, x2_old
3 x 1, x 2 tables can be renamed to x1_new, x2_new.

I hope this makes sense.

I managed to drop the tables dynamically(step 1). But impossible to apply a similar approach to rename (step 2 and 3).

Following the code gives an error: ORA-00900: invalid SQL statement

DECLARE
x NVARCHAR2 (50): = ";
y NVARCHAR2 (50): = ";
BEGIN

-Rename tables ending in 'NEW' for 'OLD '.
FOR x IN (SELECT table_name
From all_tables
Owner WHERE to = "OWNER_X."
AND SUBSTR (table_name,-3) = 'NEW')
LOOP
y: = SUBSTR (x.table_name, 1, INSTR (x.table_name, 'NEW') - 1) | 'OLD ';
EXECUTE IMMEDIATE 'Rename ' | x.table_name | « à » || y;
END LOOP;
END;
/

I read that I can't use DOF with Execute imeediate. No work around? Or different approach?

Thank you

I had where you went wrong.

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2  x NVARCHAR2(50) := '';
  3  y NVARCHAR2(50) := '';
  4  BEGIN
  5  --Rename tables that end with 'NEW' to 'OLD'
  6  FOR x IN (SELECT table_name
  7  FROM all_tables
  8  WHERE owner = 'HR'
  9  AND SUBSTR(table_name, -3) = 'OLD')
 10  LOOP
 11  y := SUBSTR(x.table_name, 1, INSTR(x.table_name, 'OLD') - 1) || 'NEW';
 12  dbms_output.put_line('ALTER TABLE ' || x.table_name || ' rename to ' || y);
 13  EXECUTE IMMEDIATE 'ALTER TABLE ' || x.table_name || ' rename to ' || y;
 14  END LOOP;
 15* END;
SQL> /
ALTER TABLE X1_OLD rename to X1_NEW
DECLARE
*
ERROR at line 1:
ORA-00900: invalid SQL statement
ORA-06512: at line 13

Now updated the code

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2  x VARCHAR2(50) := '';
  3  y VARCHAR2(50) := '';
  4  BEGIN
  5  --Rename tables that end with 'NEW' to 'OLD'
  6  FOR x IN (SELECT table_name
  7  FROM all_tables
  8  WHERE owner = 'HR'
  9  AND SUBSTR(table_name, -3) = 'OLD')
 10  LOOP
 11  y := SUBSTR(x.table_name, 1, INSTR(x.table_name, 'OLD') - 1) || 'NEW';
 12  dbms_output.put_line('ALTER TABLE ' || x.table_name || ' rename to ' || y);
 13  EXECUTE IMMEDIATE 'ALTER TABLE ' || x.table_name || ' rename to ' || y;
 14  END LOOP;
 15* END;
SQL> /
ALTER TABLE X1_OLD rename to X1_NEW
ALTER TABLE B_OLD rename to B_NEW
ALTER TABLE A_OLD rename to A_NEW

PL/SQL procedure successfully completed.

Tags: Database

Similar Questions

  • Rename the table with sys 11 g

    Hi all

    Prior to Oracle 11 g and Oracle 10 g (I think) we can rename a table like this:

    (with the user sys):

    alter table SCOTT. Rename ancien_nom NewName;

    I mean the user SYS might rename a table from another schema (for example, SCOTT)

    Now, (oracle 11g) when I try to rename a table with the user SYS to another schema, it shows me:

    SQL> conn sys as sysdba
    Enter password:
    Connected.
    SQL> rename table plantest.PEMVX_AFEPRE_ESP to PEMVX_AFEPRE_ESP_old;
    rename table plantest.PEMVX_AFEPRE_ESP to PEMVX_AFEPRE_ESP_old
           *
    ERROR at line 1:
    ORA-00903: invalid table name
    
    
    
    
    SQL> alter table plantest.PEMVX_AFEPRE_ESP to PEMVX_AFEPRE_ESP_old;
    alter table plantest.PEMVX_AFEPRE_ESP to PEMVX_AFEPRE_ESP_old
                                          *
    ERROR at line 1:
    ORA-01735: invalid ALTER TABLE option
    
    
    
    
    SQL>
    

    I read the documentation and the only way (as much, I know) rename table is that the user PLANTEST be connected to the database and rename the table. I mean, only the table owner can rename?

    Please, how can I rename ANY table of my database with the user SYS or SYSTEM?

    Thank you

    chijar,

    Please user query,

    Use the ALTER TABLE RENAME command:
    
     SQL> ALTER TABLE . RENAME TO ; 
    

    This is the keyword to rename mising is why you receive error below.

    1. SQL > alter table plantest. PEMVX_AFEPRE_ESP to PEMVX_AFEPRE_ESP_old;
    2. ALTER table plantest. PEMVX_AFEPRE_ESP to PEMVX_AFEPRE_ESP_old
    3. *
    4. ERROR on line 1:
    5. ORA-01735: invalid option of ALTER TABLE
    6. SQL >

    Naming conventions:
    -------------------

    1 names must be between 1 and 30 characters long.
    2. the names must begin with a letter.
    3. it must contain only A - Z, a - z, 0-9, _, $, and the #. You are strongly
    not recommended to use $ and #.
    4. the names are not case sensitive, unless they are created by double-quotes.

  • How to rename the table or synonym?

    Hi all

    11.2.0.1

    I want to rename the table or synonym owned by HR as a SYS user.

    SQL > rename hr.emp in hr.emp_old;

    *

    ERROR on line 1:

    ORA-01765: specifying the name of the owner of the table is not allowed

    Thank you very much

    zxy

    Hello

    You can do something like that as a user of the SYSTEM:

    1 alter session set current_schema = "HR".

    2. Rename emp emp_old

    Thank you &

    Best regards

  • RENAMECOLLECTIONTABLE - questions by renaming the table from the collection.

    Hi all

    I use the version of database Oracle 10.2.0.2, OS - Solaris

    I am enrolled in the scheme after successfully.

    <? XML version = "1.0" encoding = "UTF-8"? >
    < xs: schema xmlns: XS = "http://www.w3.org/2001/XMLSchema".
    xmlns:xdb = "http://xmlns.oracle.com/xdb".
    elementFormDefault = "qualified" attributeFormDefault = "unqualified" >

    < xs: element name = "employee" type = "employeeType" xdb:defaultTable = "EMPLOYEEXMLTYPE_TABLE" / >

    < name XS: complexType "employeeType" = >
    < xs: SEQUENCE >
    < xs: element name = "name" type = "xs: String" / >
    < xs: element name = "departments" type = "departmentsType" nillable = "true" minOccurs = "0" / >
    < xs: element name = "name" type = "xs: String" / >
    < / xs: SEQUENCE >
    < name XS: attribute = "id" type = "Integer" use = "required" / >
    < / xs: complexType >

    < name XS: complexType = "departmentsType" >
    < xs: SEQUENCE >
    < xs: element name = "Department" type = "DepartementType" maxOccurs = "unbounded" / >
    < / xs: SEQUENCE >
    < / xs: complexType >

    < name XS: complexType = "DepartementType" >
    < xs: SEQUENCE >
    < xs: element name = "name" type = "xs: String" / >
    < xs: element name = "departmentWings" type = "departmentWingsType" / >
    < / xs: SEQUENCE >
    < / xs: complexType >

    < name XS: complexType = "departmentWingsType" >
    < xs: SEQUENCE >
    < xs: element name = "departmentWing" type = "departmentWingType" maxOccurs = "unbounded" / >
    < / xs: SEQUENCE >
    < / xs: complexType >

    < name XS: complexType = "departmentWingType" >
    < xs: SEQUENCE >
    < xs: element name = "name" type = "xs: String" / >
    < xs: element name = "target" type = "xs: String" / >
    < / xs: SEQUENCE >
    < / xs: complexType >

    < / xs: Schema >

    My goal is to create indexes on/employee/departments/department/departmentWings/departmentWing/name.

    I followed the steps mentioned in Create index for an XMLType column.

    One of the measures mentioned in the link above is to rename the table in the collection (in my case departmentwing) using the DBMS_XMLSTORAGE_MANAGE procedure. RENAMECOLLECTIONTABLE.

    So I tried the following:
    SQL > START
    DBMS_XMLSTORAGE_MANAGE 2. () RENAMECOLLECTIONTABLE
    OWNER_NAME 3 = > "XDB_TEST"
    Table_name 4 = > "EMPLOYEEXMLTYPE_TABLE"
    5 COL_NAME = > NULL,
    XPATH 6 = > ' / employee/departments/Department/departmentWings/departmentWing/name. "
    7 COLLECTION_TABLE_NAME = > 'DEPTWING_TABLE '.
    (8);
    9 END;
    10.

    PL/SQL procedure successfully completed.

    If the procedure has said that it has completed successfully, I couldn't see the created DEPTWING_TABLE.

    SQL > DEPTWING_TABLE DESC;
    ERROR:
    ORA-04043: object DEPTWING_TABLE does not exist

    I am left helpless. Can someone please?

    Kind regards
    Simo.

    Published by: user5805018 on October 12, 2011 04:40

    Hello

    First of all, are you sure that the nested tables generated during the registration of the scheme?

    You must annotate the schema with xdb:storeVarrayAsTable = 'true' and use the option genTables-online true for registration.
    Then you should see something like this:

    SQL> select table_name, table_type_name, parent_table_name, parent_table_column
      2  from user_nested_tables
      3  ;
    
    TABLE_NAME                     TABLE_TYPE_NAME                PARENT_TABLE_NAME              PARENT_TABLE_COLUMN
    ------------------------------ ------------------------------ ------------------------------ --------------------------------------------------------------------------------
    SYS_NTy8fd9LOzSmun4whyTbJC4g== department384_COLL             EMPLOYEEXMLTYPE_TABLE          "XMLDATA"."departments"."department"
    SYS_NTeAiIyOu0Q9G3nvWMtcM6mQ== departmentWing381_COLL         SYS_NTy8fd9LOzSmun4whyTbJC4g== "departmentWings"."departmentWing"
     
    

    In addition, according to the manual (xdb_easeofuse_tools.pdf):

    For Oracle Database 11g Release 2 (11.2) and above, this function accepts only, XPath
    the rating as a dotted notation. If the XPath notation is used, a setting of namespaces
    may also be necessary.

    So, on your version, you must use the notation 'dot ':

    SQL> call XDB.DBMS_XMLSTORAGE_MANAGE.renameCollectionTable(
      2    USER
      3  , 'EMPLOYEEXMLTYPE_TABLE'
      4  , NULL
      5  , '"XMLDATA"."departments"."department"'
      6  , 'DEPT_TABLE'
      7  );
    
    Method called
    
    SQL> call XDB.DBMS_XMLSTORAGE_MANAGE.renameCollectionTable(
      2    USER
      3  , 'DEPT_TABLE'
      4  , NULL
      5  , '"departmentWings"."departmentWing"'
      6  , 'DEPTWING_TABLE'
      7  );
    
    Method called
    
    SQL> select table_name, table_type_name, parent_table_name, parent_table_column
      2  from user_nested_tables;
    
    TABLE_NAME                     TABLE_TYPE_NAME                PARENT_TABLE_NAME              PARENT_TABLE_COLUMN
    ------------------------------ ------------------------------ ------------------------------ --------------------------------------------------------------------------------
    DEPT_TABLE                     department384_COLL             EMPLOYEEXMLTYPE_TABLE          "XMLDATA"."departments"."department"
    DEPTWING_TABLE                 departmentWing381_COLL         DEPT_TABLE                     "departmentWings"."departmentWing"
     
    

    Hope that helps.

  • Rename the table

    How to rename the table using export and import utility?


    Thanks 10

    883532 wrote:
    How to rename the table using export and import utility?

    Thanks 10

    What is the version?

    It is not possible to rename the table using import and export, why not use SQL in 9i? its very simple
    SQL > create table emp_new in select * from emp;

    If his 10g.then above

    System $expdp / * tables = scott.emp dumpfile = emp.dmp = directory = data_pump_dir emp.log logfile
    System $impdp / * dumpfile = emp.dmp logfile = emp_imp.log remap_table = rΘpertoire emp:emp_new = data_pump_dir

    HTH.

    883532 profile
    883532
    Newbie
         
    Handle: 883532
    Status level: Beginner
    Registration date: Sep 6, 2011
    Messages total: 28
    Total Questions: 18 (12 pending)

    Keep the forum clean as much as possible by markers as replied.

    Published by: CKPT on February 20, 2012 22:00

  • Rename the table sys

    Hi all

    11.2.0.1

    How to rename scott.emp in as SYS scott.emp2?

    SQL > connect / as sysdba

    Connected.

    SQL > rename scott.emp in scott.emp2;

    *

    ERROR on line 1:

    ORA-01765: specifying the name of the owner of the table is not allowed

    Thank you all,

    pK

    Hello

    You can try-

    ALTER table scott.emp rename emp2;

    Anand

  • Rename the table require re-run the stats?

    OS: OEL (last) 64-bit
    DB: 11 GR 1 material

    I need to create a copy of a large table A in production (call of the new table B). The data are the same, but the physical layout of the tables is different (different partitioning scheme is used between table A and B).
    When I go to move table B to replace table A (which will be eventually abandoned), should I raise his stats on table B?

    It's
    (1) table to become A_OLD
    (2) table B becomes table A

    I have to raise its stats on table A in step (2)? Is there something else that needs to be done?
    (I do this to limit the impact to the GUI that uses this table - the name of the final table does not change so the GUI should not be affected).

    See you soon!

    user601798 wrote:
    Yes, I have to raise his stats on the 'new' A table even if it has the same data and the name (after renaming) but different partitioning scheme (physical model)? It seems to me that the answer would be 'yes '.
    Otherwise, not the optimizer does not fail to recognize the new partitioning?

    (1) Board toys (all data) and table Toys_New (empty)
    (2) the Toys_New table is loaded with toys data table (the two tables have the same data - only difference is partitioning)
    (2) the toys table is renamed table Toys_Old (this will be served at a later date)
    (4) table Toys_New is renamed table Toys

    And I was thinking - I forgot to talk about first-hand - Toys_New will have the same indexes, but of course the clues will have unique names from toys (to avoid the error "object already exists".

    Given that the last note, I am convinced more than stats must be run on Toys_New after step (4)

    See you soon!

    Yes, you must.

    Name of your new table is 'Toys', but this new table is totally different than the old toys. If you need to analyze the table again.

    Concerning

    Grosbois

  • How to insert rows in the table dynamically?

    I have a table with 18 ranks. Each of the first 17 rows have text read-only (mandatory activities) in a text field in the first column; the last line is a blank line with an Add button so that the user can add a new activity to the bottom of the table.

    I was invited to allow users to insert a new activity at any location in the table (by example, to insert a new 7 row after row 6), instead of just at the end of the table. (Of course this requires also remove the text in the text box of the activity and to change to the entry by the user, as well as a few other household activities, which should not be a problem if I can get just the line darn insert!)

    I searched the forums and looked at some of the examples ensure dynamics. From what I've seen, this should be fairly simple... but it does not work.

    Or rather, it works very well on the first line, several times even. I have a message box displays the indices of current and new line properly for each line. But in any other line as the first, insertInstance() gives me an error "index value is out of range" even when the message box displays the correct clues.

    Given the structure and the following code in each button an ActivityAdd line, can someone tell me what I'm doing wrong? Thank you!!!

    -------------------------------------------

    tableStructure.png

    ---------------------------------------------

    T_Activities.R_Activity [0]. T_AddRemove.R_Add.B_ActivityAdd::click - (JavaScript, client)

    var vNewRowIndex = this.parent.parent.parent.index + 1;

    xfa.host.messageBox ("the index of this line:" + this.parent.parent.parent.index + ".") Index of the new row: "+ vNewRowIndex +". ");"

    T_Activities.R_Activity.instanceManager.insertInstance (vNewRowIndex, 1);

    ------------------------------------------------

    FWIW, I've seen the warnings about the instance manager and features underscores in names of object... for better or worse, * all * of my names object * all * of my forms use underscores like this, and to the best of my knowledge I have never had a problem. So which proves to be the problem here, I'll gladly change the names in the table.

    Did you use java or formcalc when you got the error? I've recreated your table and the code works in formcalc.

    Concerning the insertIndex working in the first line, I think that maybe it's because you always refer to the first line (R_Activity).

  • Access to the table in paragraph index loop (ID CC 2014)

    Initially, I wanted to look for every blank line in the page (those who only have a return '\r') and remove the carriage return character in order to remove the blank line.

    Here's what I have so far:

    var myParagraphs = app.activeDocument.textFrames [0].paragraphs.everyItem ();

    for (i = 0; i < myParagraphs.length; i ++) {}

    myChar = myParagraphs [i] .characters var [0];

    If (myChar.contents == '\r') {}

    App.Select (myChar);

    App.Cut ();

    }

    }

    This out errors (it does not what I expected it to do).

    By running in the ESTK gives the error 'the object does not support the property or method '0' ' at line 3.

    If I have a particular character as reference:

    var myCharacter = app.activeDocument.textFrames [0] .paragraphs [0] .characters [0]

    If (myCharacter.contents == '\r') {}

    App.Select (myChar);

    App.Cut ();

    }


    I get success.


    paragraphs.everyItem () generates an array of points and I can confirm that with:

    Alert (myParagraphs.Contents);


    I'm referencing the table incorrectly?



    NO.

    The problem is that the real object 'Paragraphs' changes as soon as you take just a single '\r' - the number of changes in paragraphs! Your "myParagraphs" temp table always contains a copy of the document as they were, but as soon as you take a return, it no longer matches what is actually in the document.

    I have two different solutions for you, choose one:

    1 loop backwards over the table of myParagraphs instead of to the front. If you delete a return, just in front of it is more valid - but everything behind it will be valid.

    A loop backwards on a 'live' table is a trick that works with all objects. It will come handy repeatedly.

    2. (I think I would prefer this in this situation) without having to "manually" loop over all paragraphs and verification of its characters. Simply use GREP! Search the '(?<=\r)\r+'> ) ( without the quotes, as always) and replace it with nothing. Fact.

    Free tip of the day: do not define a selection and use "app.cut ()". You can simplify this to "myParagraphs [i] .characters [0] .remove (); It should be much faster than the selection and then using cut. (And why to use "cut" when you want "app.clear ()"? (After checking: Oops.) Seems that someone at Adobe forgot to include that!)

  • We can change the advance in the region of the table dynamically properties

    Hello

    I'm changing region of type advance table properties dynamically. I need to change the name of VO dynamically and execute the vo.executequery. Is this possible?


    Thank you

    Re: Instance of the View object

    Thank you
    -Anil

  • Form on the report on the table - dynamically change the region title

    I use a form on a report on a table to give me the standard features of add/change/delete a table.

    When I get to the page of the form, I want my title to say the addition or modification, whether I came to the form via clicking on the link change or create new button to the page.

    I thought it would be easy but I have mingled around with calculations, define objects hidden, etc., but can't seem to make it work.

    Is there a quick process, I can follow to get there? My first thought was to create a component hidden on my page of form and that fill the homepage depending on whether I clicked on edit or create. But the title of region doesn't seem to capture the hidden element. I put the title of region: P1_HIDDEN_ITEM_TITLE. and that is the question that I put my home page. The hidden element is fine but never gets picked up in the title.

    Specify the title of region

    & P1_HIDDEN_ITEM_TITLE.

    CITY

  • import to another name of the table.

    I need to import them from a full export file, a single table 'testtable' of the user "testuser" to the same user, but a different table name. Is this possible? What is the syntax of import for this? Thank you.

    BB

    What version of oracle (4 digits)?

    I'm sure that he will not enter the sys tablespace?

    Sure. He will not enter sys tablespace. You just rename the table and moving not the table to a different tablespace.

    select table_name,tablespace_name from dba_tables where owner='OPS$USER';
    
    TABLE_NAME                     TABLESPACE_NAME
    ------------------------------ ------------------------------
    TEMP_TAB2                     USERS
    
    SQL> alter table ops$user.TEMP_TAB2 rename to TEMP_TAB3;
    
    Table altered.
    
    select table_name,tablespace_name from dba_tables where owner='OPS$USER';
    
    TABLE_NAME                     TABLESPACE_NAME
    ------------------------------ ------------------------------
    TEMP_TAB3                      USERS
    

    HTH
    -André

  • Creating a view of mast bearing the same name with the name of the table

    Hello world

    After having dropped the name of view mast QQ. "" TRDLN_DIM_MV ", I try to recreate by having the same name as the table, but I got an error message stating that"the name is already used by an existing object. How to create a matte view to resemble the object possessed by schema 'GQ', with a name identical to the name of the table, drop the table?

    OWNER_O CREATED LAST_DDL_ TIMESTAMP STATUS OBJECT_NAME OBJECT_TYPE
    ===
    GQ MATERIALIZED VIEW 5 NOVEMBER 08 2008 13 DECEMBER 08-11 - 05:06:31:46 TRDLN_DIM_MV VALID
    GQ TABLE 5 NOVEMBER 08 2008 15 DECEMBER 08-11 - 05:06:31:00 TRDLN_DIM_MV VALID
    QQ TABLE 17 AUGUST 07 16 MARCH 09 2007-08 - 17:17:05:21 TRDLN_DIM_MV VALID

    SQL & gt; CREATE THE QQ MATERIALIZED VIEW. TRDLN_DIM_MV
    2 TABLESPACE FPLC01S
    3 NOCACHE
    4 NOLOGGING
    5 PACK
    6 PARALLEL (DEGREE 1 2 INSTANCES)
    7 BUILD IMMEDIATE
    8 REFRESH FORCE ON REQUEST
    9 WITH PRIMARY KEY
    10 AS
    11 Select / * + NO_REWRITE * / trdln_id,.
    trdln_skid 12,
    trdln_end_date 13,
    FIRST_VALUE (trdln_end_date) 14 over (partition by order of trdln_id trdln_end_date DESC) as MaxDate
    15 QQ.trdln_dim;
    of QQ.trdln_dim
    *
    ERROR on line 15:
    ORA-00955: name is already used by an existing object

    I appreciate your help. Thanks in advance!

    Kind regards
    Radic

    You must rename the table (using ALTER TABLE TableName RENAME to othername ;) before creating the MV.
    You cannot have both existing with the same name in the same schema.

    What you see in the scheme of GQ, is that the 'picture' is the underlying table for the materialized view.
    When you do a MY_MV CREATE MATERIALIZED VIEW AS...
    Oracle creates two objects with the same name, we the MY_MV materialized view, and the other being the MY_MV table, which is the physical storage. {Don't forget that a materialized view is a way to get a physical representation of views.}

    In the schema QQ is what you
    a. a real Table
    or
    b. a MV incorrectly deleted, resulting in the underlying Table always presents.

  • How can I change the tables

    I have an app that is finished and now says the ADMIN I have to rename all the tables.
    What I have to redo the entire application?

    Hello

    I have never renamed a few items and found it easier to do an export of the application and then go through the sql file to identify the pages that were the elements, and then go to each page and change the appropriate objects. It could be that you might actually updated the export file itself and then re - import. You will need to ensure that you only replace the table names and allow for the fact that the export file may contain table names spread over two lines - you then to import into a new application id (just in case!)

    Another suggestion, perhaps, would be to rename the tables and then create using the original names of synonyms?

    Andy

  • insert into the table of a collection

    Hello

    I have a table with the following structure



    CREATE TABLE TAB_A
    (
    x NUMBER (30.0),.
    Y NUMBER (30.0),.
    z NUMBER (30.0)
    )

    Now I have a collection which I already loaded with the data in my program and its definition is the following


    TYPE tab_A_type_rec IS RECORD)
    x NUMBER (30.0),.
    Y NUMBER (30.0),.
    z NUMBER (30.0)
    );

    TYPE tab_A_type IS TABLE OF THE tab_A_type_rec;

    pkv_A_tab tab_A_type: = tab_A_type ();

    in my program, I have charge collection pkv_A_tab with the following data


    pkv_A_tab

    x y z

    1 2 3
    4 5 6
    7 8 9

    Now how to insert this data into the TAB_A table


    I'm a loop like this, but I'm not able to find the insert statement


    for me in pkv_A_tab.first... loop of pkv_A_tab. Last
    -What would be the insert statement to load the table here?
    end loop;


    Thank you
    Pramod

    Like this?

     INSERT INTO tab_a
        VALUES pkv_A_tab (i);
    

    G.

Maybe you are looking for