?
Solved

Does anyone have PL/SQL code to add new field in an Oracle  table with specific order?

Posted on 2014-12-29
27
Medium Priority
?
240 Views
Last Modified: 2015-01-09
I need to add fields to tables and I do not want them added to the end of the table order.  I know that I can create temp table, add the field, and then repopulate from the temp table.  This is tedious.

I would like a procedure that would take in a schema, table_name, new_field_name, new_field_type, and order_number.  Then, the new field would be added to the table and the table would maintain constraints/grants/triggers etc.


Does anyone know if this exists already?
0
Comment
Question by:kfeiner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +3
27 Comments
 
LVL 13

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 999 total points
ID: 40522551
Then, the new field would be added to the table and the table would maintain constraints/grants/triggers etc.
"ALTER TABLE ADD ... " would not necessarily render those objects invalid. Apart from that: what exactly are you aiming at?! I am afraid there is no way adding a column "in between" ...
If you're having problems taking a table "offline" for reordering purposes (via temp table), please have a loom at DBMS_REDEFINITION" and/or EBR (Edition based redefinition)...
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 501 total points
ID: 40522684
0
 

Author Comment

by:kfeiner
ID: 40522705
Im not concerned with taking the table offline.  I am concerned with the tedious nature of creating temp tables, grabbing grants, dropping temp_tables etc... everytime I want to add a field.

Will DBMS_REDEFINITION  allow me to add a field to an existing table in an order of my choosing?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 999 total points
ID: 40522727
Will DBMS_REDEFINITION  allow me to add a field to an existing table in an order of my choosing?
Short answer: yes
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40522731
I recommend, you take a look at editionable views (when using EBR) ;-)
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40522781
Personally I would forget about columns in specific positions.  There is really no need for doing it today.  Maybe back in the infancy of databases there might be some performance or storage arguments but those days are long gone.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 40523190
It can be done as said above or even by proc if you google it out. But getting curios as to why you need fields only in a given order as it does not matter for anything right ? any particular reasons behind.

thanks
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 40523409
i bet the columns are in alphabetical order ... ? no ?
some front-ends like toad can display a table with the column names ordered.

it doesn't matter what the order of columns is physically for the database.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40523891
Yes, column order within a table really doesn't matter anymore, but if you need some kind of sorting (apart from "built-in" ordering in some IDEs), you may query "user_tab_columns" or "dba_tab_columns" and filter/order by "column_id" or "column_name" or or or....
0
 

Author Comment

by:kfeiner
ID: 40523897
The reason for the order is so that a set of fields are listed last in the table.  We have a database standard of listing the timestamp, last_updated, user etc at the end of the tables.  Its more consistent when running adhoc queries.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40523903
Then why not just use a view "instead"?!
0
 

Author Comment

by:kfeiner
ID: 40524231
Dont get hung up on the "WHY" I want a generic utility to add fields in a specific order.   Im not going to add views for 15 different tables just for ordering.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40524436
>>Its more consistent when running adhoc queries.

Column order has nothing to do with consistency of queries.

>> I want a generic utility to add fields in a specific order.  

This was already provided:  DBMS_REDEFINITION.
0
 

Author Comment

by:kfeiner
ID: 40524444
The consistency Im speaking of is that of the table structure with the last 4 fields being similar across tables.   I have seen this practice in many shops.  

I will look further into DBMS_REDEFINITION, as I have yet to see if this will suit my need any easier than  creating temp tables, grabbing grants, dropping temp_tables etc... everytime I want to add a field.
0
 

Author Comment

by:kfeiner
ID: 40524450
I will look further into DBMS_REDEFINITION, as I have yet to see if this will suit my need any easier than  creating temp tables, grabbing grants, dropping temp_tables etc... everytime I want to add a field.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40524565
This was already provided:  DBMS_REDEFINITION.
YES, it has been provided! By ??!! Most of the time I swallow the pill when it comes to points distribution, but this one here is absolutely ridiculous! Giving away the whole 500 pts for just 2 blind links???!!! That can't be true!!
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40524679
Alex,

If you have an issue with how a question was closed please click the 'Request Attention' link and explain the issues and a Moderator will take a look.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40525735
2 blind links???
What really matters is the content and accuracy of the links.
For example, the second link points to a fine EE article by Greg Clough detailing the best method to make fundamental changes to Oracle tables.
;)
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40526595
@MikeOM_DBA:

http://www.experts-exchange.com/Database/Oracle/Q_28587953.html#a40522684

Just 2 BLIND links. Period. No explanation at all, just links. Period.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40528285
Perhaps the links are self-explanatory?
;)
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40528394
No further questions, your honor ;-)
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 40533087
>>  I have seen this practice in many shops.  
there is a lot of shops where the design is beautiful and where the performance is appalling !

do you really want a production plant having to go down for just adding a column to a table ?
any good ceo will wipe a request for having columns in a specific order off the table for oracle db's.
the costs to keep those 4 columns last in the table will never be beneficial
0
 

Author Comment

by:kfeiner
ID: 40533535
You must understand that I do not create the standards, but I must follow them.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question