COPY FROM database TO database action - destination_table (column_name, column_name, - column_name ...) USING query
Here is a sample COPY command:
COPY FROM SCOTT/TIGER@BOSTONDB - TO TODD/FOX@CHICAGODB - CREATE NEWDEPT (DNUMBER, DNAME, CITY)- USING SELECT * FROM DEPT
To specify a database in the FROM or TO clause, you must have a valid username and password for the local and remote database(s) and know the appropriate database specification(s). COPY obeys Oracle security, so the username you specify must have been granted access to tables for you to have access to tables. For information on what databases are available to you, contact your DBA.
When you copy to your local database from a remote database, you can omit the TO clause. When you copy to a remote database from your local database, you can omit the FROM clause. When you copy between remote databases, you must include both clauses.
The COPY command behaves differently based on whether the destination table already exists and on the action clause you enter (CREATE in the example above). See "Controlling Treatment of the Destination Table" later in this chapter.
By default, the copied columns have the same names in the destination table that they have in the source table. If you want to give new names to the columns in the destination table, enter the new names in parentheses after the destination table name. If you enter any column names, you must enter a name for every column you are copying.
Note: To enable the copying of data between Oracle and non-Oracle databases, NUMBER columns are changed to DECIMAL columns in the destination table. Hence, if you are copying between Oracle databases, a NUMBER column with no precision will be changed to a DECIMAL(38) column. When copying between Oracle databases, you should use SQL commands (CREATE TABLE AS and INSERT) or you should ensure that your columns have a precision specified.
The USING clause specifies a query that names the source table and specifies the data that COPY copies to the destination table. You can use any form of the SQL SELECT command to select the data that the COPY command copies.
Here is an example of a COPY command that copies only two columns from the source table, and copies only those rows in which the value of DEPTNO is 30:
SQL> COPY FROM SCOTT/TIGER@BOSTONDB - > REPLACE EMPCOPY2 - > USING SELECT ENAME, SAL - > FROM EMPCOPY - > WHERE DEPTNO = 30
You may find it easier to enter and edit long COPY commands in command files rather than trying to enter them directly at the command prompt.
The REPLACE clause names the table to be created in the destination database and specifies the following actions:
To copy EMP from a remote database into a table called EMPCOPY on your own database, enter the following command:
Note: See your DBA for an appropriate username, password, and database specification for a remote computer that contains a copy of EMP.
SQL> COPY FROM SCOTT/TIGER@BOSTONDB - > CREATE EMPCOPY - > USING SELECT * FROM EMP
SQL*Plus displays the following messages:
Array fetch/bind size is 20. (arraysize is 20) Will commit when done. (copycommit is 0) Maximum long size is 80. (long is 80)
SQL*Plus then creates the table EMPCOPY, copies the rows, and displays the following additional messages:
Table EMPCOPY created. 14 rows selected from SCOTT@BOSTONDB. 14 rows inserted into EMPCOPY. 14 rows committed into EMPCOPY at DEFAULT HOST connection.
In this COPY command, the FROM clause directs COPY to connect you to the database with the specification D:BOSTON-MFG as SCOTT, with the password TIGER.
Notice that you do not need a semicolon at the end of the command; COPY is a SQL*Plus command, not a SQL command, even though it contains a query. Because most COPY commands are longer than one line, you must use a hyphen (-), optionally preceded by a space, at the end of each line except the last.
The variable ARRAYSIZE limits the number of rows that SQL*Plus fetches from the database at one time. This number of rows makes up a batch. The variable COPYCOMMIT sets the number of batches after which COPY commits changes to the database. (If you set COPYCOMMIT to zero, COPY commits changes only after all batches are copied.) For more information on the variables of the SET command, including how to change their settings, see SET.
After listing the three system variables and their values, COPY tells you if a table was dropped, created, or updated during the copy. Then COPY lists the number of rows selected, inserted, and committed.
For example, to make a local copy of a table named DEPT, owned by the username ADAMS on the database associated with the SQL*Net connect string BOSTONDB, you would enter
SQL> COPY FROM SCOTT/TIGER@BOSTONDB - > CREATE EMPCOPY2 - > USING SELECT * FROM ADAMS.DEPT
Of course, you could get the same result by instructing COPY to log in to the remote database as ADAMS. You cannot do that, however, unless you know the password associated with the username ADAMS.