Home » SQL & PL/SQL » SQL & PL/SQL » copy one table to another table (hp unix)
copy one table to another table [message #638476] Fri, 12 June 2015 06:44 Go to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
hi all, it would be great if anyone can help me out in this issue.

There are 30,000 table present in the schema. where the user will passing the source and destination table as input.

i am wrriting a shell script to copy the one table data to the another table by overwritten.

could you please provide the oracle query to copy one table to another table.



Re: copy one table to another table [message #638477 is a reply to message #638476] Fri, 12 June 2015 06:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/

>could you please provide the oracle query to copy one table to another table.
What to do when the number of columns or the column datatypes do not match?
Re: copy one table to another table [message #638479 is a reply to message #638476] Fri, 12 June 2015 06:48 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
assuming the ddl in both tables is identical:
insert into tablex select * from tabley;

HOWEVER:
What on earth are you doing with 30000 tables and why are you copying data from one table to another?
Re: copy one table to another table [message #638480 is a reply to message #638477] Fri, 12 June 2015 06:54 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
thanks for providing the guidelines. i will make sure i will be following the rules. [blackswan].

I need to provide a script to the user. There 30,0000 tables, user will taking any one source table & copy the same data to any other requested table.

So i am planning to write a shell script for this request. [pablolee]
Re: copy one table to another table [message #638481 is a reply to message #638480] Fri, 12 June 2015 06:57 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Which Oracle user owns source table?
Which Oracle user owns target table?
What to do if target table already exists?
Re: copy one table to another table [message #638482 is a reply to message #638480] Fri, 12 June 2015 06:58 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
All you have done is reiterate what you originally asked. You provided NO additional information. The point behind my question is
1. It sounds like a ridiculous request. Copying an entire table into another table does nothinbg but duplicate data. What's the point of that?
2. 30000 sounds like a MASSIVE number of tables, it sounds like there have been (and continue to be) some very poor design decisions happening there.

Also, you didn't answer BlackSwan's very pertinent question
"What to do when the number of columns or the column datatypes do not match?"

[Updated on: Fri, 12 June 2015 06:59]

Report message to a moderator

Re: copy one table to another table [message #638483 is a reply to message #638480] Fri, 12 June 2015 06:59 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
tablex -> ram,kk,gg [source table]
tabley -> ram,ll,oo,mm [destination table]

insert into tabley select * from tablex;

In this case, after the above query. I believe the tabley table will be overwritten ?

will the result be :

tablex -> ram,kk,gg
tabley -> ram,ll,oo

or

tabley -> ram,ll,oo,mm,kk,gg
Re: copy one table to another table [message #638484 is a reply to message #638483] Fri, 12 June 2015 07:01 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
What do you mean by "I believe the tabley table will be overwritten ?"
Are you saying that your tables are named with commas in the name? (Hint: Thants a horrible practice)

[Updated on: Fri, 12 June 2015 07:02]

Report message to a moderator

Re: copy one table to another table [message #638485 is a reply to message #638482] Fri, 12 June 2015 07:01 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
Which Oracle user owns source table?

root user

Which Oracle user owns target table?

root user.

What to do if target table already exists?

it can be overwritten.

I have the root user acess.
Re: copy one table to another table [message #638486 is a reply to message #638485] Fri, 12 June 2015 07:02 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
"What to do when the number of columns or the column datatypes do not match?"

First i need to check whether the no of columns and datatypes matches. if yes it should copy if not it should throw an error.

[Updated on: Fri, 12 June 2015 07:03]

Report message to a moderator

Re: copy one table to another table [message #638487 is a reply to message #638483] Fri, 12 June 2015 07:03 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>insert into tabley select * from tablex;
>In this case, after the above query. I believe the tabley table will be overwritten ?

You believe wrongly!
INSERT only adds new rows & has NO impact on existing rows.

Since you choose to not answer our question, I choose to ignore your questions.
Re: copy one table to another table [message #638488 is a reply to message #638483] Fri, 12 June 2015 07:03 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
orclz>
orclz> conn scott/tiger
Connected.
orclz> desc emp;
 Name                                                        Null?    Type
 ----------------------------------------------------------- -------- -----------------
 EMPNO                                                       NOT NULL NUMBER(4)
 ENAME                                                                VARCHAR2(10)
 JOB                                                                  VARCHAR2(9)
 MGR                                                                  NUMBER(4)
 HIREDATE                                                             DATE
 SAL                                                                  NUMBER(7,2)
 COMM                                                                 NUMBER(7,2)
 DEPTNO                                                               NUMBER(2)

orclz> desc dept;
 Name                                                        Null?    Type
 ----------------------------------------------------------- -------- -----------------
 DEPTNO                                                      NOT NULL NUMBER(2)
 DNAME                                                                VARCHAR2(14)
 LOC                                                                  VARCHAR2(13)

orclz> insert into emp select * from dept;
insert into emp select * from dept
            *
ERROR at line 1:
ORA-00947: not enough values


orclz>
Re: copy one table to another table [message #638489 is a reply to message #638485] Fri, 12 June 2015 07:04 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
1. Once again, what EXACTLY do you mean by 'overwritten'. Simply inserting data into a table will not overwrite that table (whatever 'overwrite' means, unless it meanst 'insert into', in which case, just say 'insert into') Try not to make up your own terms.
2. Root user? Are you saying that you have a user called root? Or are you implying that the tables are owned by sys?
Re: copy one table to another table [message #638490 is a reply to message #638489] Fri, 12 June 2015 07:14 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
1. Once again, what EXACTLY do you mean by 'overwritten'. Simply inserting data into a table will not overwrite that table (whatever 'overwrite' means, unless it meanst 'insert into', in which case, just say 'insert into') Try not to make up your own terms.

If tablex is copied to tabley. tabley should be same as tablex. previous data of tablex can be dropped.
2. Root user? Are you saying that you have a user called root? Or are you implying that the tables are owned by sys?

its owned by sys.
Re: copy one table to another table [message #638491 is a reply to message #638490] Fri, 12 June 2015 07:20 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Quote:

If tablex is copied to tabley. tabley should be same as tablex. previous data of tablex can be dropped.
As Blackswan mentioned above. Your assumption is incorrect. the data will be ADDED/APPENDED to the existing data.
Quote:
its owned by sys.
Do NOT do this! it is an horrendous idea Michel can no doubt link you to one of his lists as to why it is a horrible idea. Over to you Mr C Wink )

ETA:
Actually, to save Michel having to do it:
Quote:
Michel Cadot writes (on far too many occassions Smile )
- Never ever use SYS (or SYSDBA) but for maintenance purpose (startup, shutdown, backup, recover)
- SYS/SYSDBA is special
- SYS/SYSDBA is Oracle proprietary (try to open a SR/TAR starting with "i did that with SYS/SYSDBA" and you'll see the immediate answer)
- SYS/SYSDBA does not act like any other user
- When you use SYS/SYSDBA Oracle deactivates some code path and activates others
- Whatever you do with SYS/SYSDBA will neither validate nor invalidate the same thing with any other user.

NEVER EVER use SYS/SYSDBA for anything that can be done by another user.
Use SYS/SYSDBA ONLY for something that can't be done by someone else.


ETA2: Some more detail:
http://www.orafaq.com/forum/t/164126/0/

[Updated on: Fri, 12 June 2015 07:36]

Report message to a moderator

Re: copy one table to another table [message #638492 is a reply to message #638491] Fri, 12 June 2015 07:24 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
All of these discussion miss (apart from an initial comment on) the fact that this process that you intend seems to be pretty awful. Can you explain exactly what business scenario is happening here.
Re: copy one table to another table [message #638495 is a reply to message #638491] Fri, 12 June 2015 08:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I was not there today but I see you have found the link. Smile

Re: copy one table to another table [message #638496 is a reply to message #638495] Fri, 12 June 2015 08:27 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Aye, a very quick search (which TBH I should've done first rather than hitting you up for it Smile ). It's now in my favourites folder ready to be wheeled out when I see the devs here trying anything like that.
Re: copy one table to another table [message #638497 is a reply to message #638492] Fri, 12 June 2015 08:28 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
sure I wont use sys.

thanks for your information.

I am writiting a tool for the users. To copy the previous data to the current table. Manually some times users will copying the data manually like this.
Re: copy one table to another table [message #638498 is a reply to message #638497] Fri, 12 June 2015 08:34 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
arun888 wrote on Fri, 12 June 2015 14:28
I am writiting a tool for the users. To copy the previous data to the current table. Manually some times users will copying the data manually like this.

So do you then drop the 'previous data' table? How many tables are there with identical structure? Why does data get loaded into one table and then bounced into another table? Why not loaded directly into the 'final destination' missing out the seemingly unneccesary 'staging' table.
Has this set of tables actually been built yet?
Re: copy one table to another table [message #638500 is a reply to message #638498] Fri, 12 June 2015 09:05 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Maybe the users have shares in the storage vendors...
Re: copy one table to another table [message #638501 is a reply to message #638498] Fri, 12 June 2015 09:15 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
Correct me if I am wrong, but I suspect the reason to copy these tables is to populate some king of
test database? If the test DB is a clone of the source DB, copying the data from one db to another
may not be enough, what about triggers that would need to be disabled? or sequences? or FK
constraints?
I faced similar issue where a set of "common" tables needed to constantly be "synchronized" with prod in the test and QA database(s), and I wrote a script like the attached.
Script is based on a source (organization external) table with the list of candidate tables and in the following format (you could create permanent table):
#:############################################################
#: ResyncTab.dat
#:
#: List of tables and corresponding sequences to be re-sync'd
#:
#:------------------------------------------------------------
#: Change the first field to '1' to select table(s) to synch.
#:------------------------------------------------------------
#:FLAG:TABLE:SEQUENCE_NAME:
0:'ACTIVITY_CODES':'ACTIVITY_CODES_SEQ':
0:'APPLICATIONS':'APP_ID_SEQ':
0:'BARCODES'::
1:'CODE_TABLE':'CODE_TABLE_SEQ':
1:'CODE_TYPES':'CODE_TYPES_SEQ':
0:'CONTACT_SUB_TYPE':'CONTACT_SUB_TYPE_SEQ':
0:'CONTACT_TYPE':'CONTACT_TYPE_SEQ':
0:'COVER_COMMENTS'::
0:'EDOCUMENT'::
0:'EQUESTIONS'::
# - - -  e t c  - - -

See script attached.

[Updated on: Fri, 12 June 2015 09:27]

Report message to a moderator

Re: copy one table to another table [message #638507 is a reply to message #638501] Fri, 12 June 2015 10:06 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
LKBrwn_DBA wrote on Fri, 12 June 2015 19:45
Correct me if I am wrong, but I suspect the reason to copy these tables is to populate some king of
test database?


After reading the entire thread, I didn't find any clue about different databases being involved. In fact, OP is trying to do something silly in the same schema as I understand.
Re: copy one table to another table [message #638520 is a reply to message #638507] Fri, 12 June 2015 14:47 Go to previous messageGo to next message
saadatahmad
Messages: 452
Registered: March 2005
Location: Germany/Paderborn
Senior Member

Waooo...why on earth someone will create 30000 tables in a database!!
Re: copy one table to another table [message #638521 is a reply to message #638520] Fri, 12 June 2015 14:48 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Nothing special. Your typical EBS database will have double that.
Re: copy one table to another table [message #638924 is a reply to message #638521] Thu, 25 June 2015 08:29 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/m/638921/#msg_638921

COPYING tables is NOT the smart or efficient way to migrate any database.
Re: copy one table to another table [message #638955 is a reply to message #638924] Fri, 26 June 2015 09:56 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
are you sure hes talking oracle tables and not unix tables. He said the owner of the tables is ROOT which implies Unix tables.
Re: copy one table to another table [message #638957 is a reply to message #638955] Fri, 26 June 2015 11:26 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
He corrected himself, here, saying that the owner is SYS.
Re: copy one table to another table [message #639835 is a reply to message #638520] Fri, 17 July 2015 09:45 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
saadatahmad wrote on Fri, 12 June 2015 15:47
Waooo...why on earth someone will create 30000 tables in a database!!

SAP has ~44,000 tables.
Razz
Re: copy one table to another table [message #639857 is a reply to message #639835] Sat, 18 July 2015 02:39 Go to previous message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
LKBrwn_DBA wrote on Fri, 17 July 2015 15:45
saadatahmad wrote on Fri, 12 June 2015 15:47
Waooo...why on earth someone will create 30000 tables in a database!!

SAP has ~44,000 tables.
Razz

Yeah, but SAP is just a big ol' show off, everyone knows that, always boasting about how many tables it's got. Wink
Previous Topic: 25% lowest sal
Next Topic: procedure error ger end of file
Goto Forum:
  


Current Time: Thu Apr 25 04:53:05 CDT 2024