Oracle Copy Command

  • Uploaded by: Raj
  • 0
  • 0
  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Oracle Copy Command as PDF for free.

More details

  • Words: 2,208
  • Pages: 8
Oracle SQL*Plus COPY Command

The COPY command will be obsoleted in future releases of SQL*Plus. COPY supports the datatypes listed for the COPY command, but no new datatypes will be supported. COPY Command Syntax

COPY {FROM database | TO database | FROM database TO database} {APPEND|CREATE|INSERT|REPLACE} destination_table [(column, column, column, ...)] USING query where database has the following syntax: username[/password]@connect_identifier Copies data from a query to a table in a local or remote database. COPY supports the following datatypes: CHAR DATE LONG NUMBER VARCHAR2

Terms Refer to the following list for a description of each term or clause: FROM database The database that contains the data to be copied. If you omit the FROM clause, the source defaults to the database to which SQL*Plus is connected (that is, the database that other commands address). You must use a FROM clause to specify a source database other than the default. TO database The database containing the destination table. If you omit the TO clause, the destination defaults to the database to which SQL*Plus is connected (that is, the database that other commands address). You must use a TO clause to specify a destination database other than the default. database Specifies username[/password] @connect_identifier of the Oracle source or destination database you wish to COPY FROM or COPY TO. If you do not specify password in

~Ramesh Raj

Oracle SQL*Plus COPY Command

either the COPY FROM clause or the COPY TO clause, SQL*Plus will prompt you for it. SQL*Plus suppresses the display of your password response. You must include the connect_identifier clause which consists of an Oracle Net connection string, to specify the source or destination database. The exact syntax depends upon the Oracle Net communications protocol your Oracle installation uses. For more information, refer to the Oracle Net manual appropriate for your protocol or contact your DBA. APPEND Inserts the rows from query into destination_table if the table exists. If destination_table does not exist, COPY creates it. CREATE Inserts the rows from query into destination_table after first creating the table. If destination_table already exists, COPY returns an error. INSERT Inserts the rows from query into destination_table. If destination_table does not exist, COPY returns an error. When using INSERT, the USING query must select one column for each column in the destination_table. REPLACE Replaces destination_table and its contents with the rows from query. If destination_table does not exist, COPY creates it. Otherwise, COPY drops the existing table and replaces it with a table containing the copied data. destination_table Represents the table you wish to create or to which you wish to add data. (column, column, column, ...) Specifies the names of the columns in destination_table. You must enclose a name in double quotes if it contains lowercase letters or blanks. If you specify columns, the number of columns must equal the number of columns selected by the query. If you do not specify any columns, the copied columns will have the same names in the destination table as they had in the source if COPY creates destination_table. USING query

~Ramesh Raj

Oracle SQL*Plus COPY Command

Specifies a SQL query (SELECT command) determining which rows and columns COPY copies.

Usage 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 SQL*Plus SET LONG variable limits the length of LONG columns that you copy. If any LONG columns contain data longer than the value of LONG, COPY truncates the data. SQL*Plus performs a commit at the end of each successful COPY. If you set the SQL*Plus SET COPYCOMMIT variable to a positive value n, SQL*Plus performs a commit after copying every n batches of records. The SQL*Plus SET ARRAYSIZE variable determines the size of a batch. Some operating environments require that service names be placed in double quotes.

Examples The following command copies the entire EMPLOYEES table to a table named WESTEMPLOYEES. Note that the tables are located in two different databases. If WESTEMPLOYEES already exists, SQL*Plus replaces the table and its contents. The columns in WESTEMPLOYEES have the same names as the columns in the source table, EMPLOYEES.

COPY FROM HR/your_password@HQ TO JOHN/your_password@WEST REPLACE WESTEMPLOYEES USING SELECT * FROM EMPLOYEES

The following command copies selected records from EMPLOYEES to the database to which SQL*Plus is connected. SQL*Plus creates SALESMEN through the copy. SQL*Plus copies only the columns EMPLOYEE_ID and LAST_NAME, and at the destination names them EMPLOYEE_ID and SA_MAN.

COPY FROM HR/your_password@ORACLE01 CREATE SALESMEN (EMPLOYEE_ID, SA_MAN) -

~Ramesh Raj

Oracle SQL*Plus COPY Command USING SELECT EMPLOYEE_ID, LAST_NAME FROM EMPLOYEES WHERE JOB_ID='SA_MAN';

Copying Data from One Database to Another Use the SQL*Plus COPY command to copy CHAR, DATE, LONG, NUMBER or VARCHAR2 data between databases and between tables on the same database. With the COPY command, you can copy data between databases in the following ways: 

Copy data from a remote database to your local database.



Copy data from your local (default) database to a remote database (most systems).



Copy data from one remote database to another remote database (most systems).

Note: In general, the COPY command was designed to be used for copying data between Oracle and non-Oracle databases. You should use SQL commands (CREATE TABLE AS and INSERT) to copy data between Oracle databases.

Understanding COPY Command Syntax You enter the COPY command in the following form: COPY FROM database TO database action destination_table (column_name, column_name, column_name ...) USING query

Here is a sample COPY command: COPY FROM HR/your_password@BOSTONDB TO TODD/your_password@CHICAGODB CREATE NEWDEPT (DEPARTMENT_ID, DEPARTMENT_NAME, CITY) USING SELECT * FROM EMP_DETAILS_VIEW

To specify a database in the FROM or TO clause, you must have a valid username and password for the local and remote databases and know the appropriate Oracle Net service names. 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

~Ramesh Raj

Oracle SQL*Plus COPY Command

FROM clause. When you copy between remote databases, you must include both clauses. However, including both clauses increases the readability of your scripts. 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). 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 DEPARTMENT_ID is 30: COPY FROM HR/your_password@BOSTONDB REPLACE EMPCOPY2 USING SELECT LAST_NAME, SALARY FROM EMP_DETAILS_VIEW WHERE DEPARTMENT_ID = 30

You may find it easier to enter and edit long COPY commands in scripts rather than trying to enter them directly at the command prompt.

Controlling Treatment of the Destination Table You control the treatment of the destination table by entering one of four control clauses-REPLACE, CREATE, INSERT, or APPEND. The REPLACE clause names the table to be created in the destination database and specifies the following actions:

~Ramesh Raj

Oracle SQL*Plus COPY Command



If the destination table already exists, COPY drops the existing table and replaces it with a table containing the copied data.



If the destination table does not already exist, COPY creates it using the copied data.

You can use the CREATE clause to avoid accidentally writing over an existing table. CREATE specifies the following actions: 

If the destination table already exists, COPY reports an error and stops.



If the destination table does not already exist, COPY creates the table using the copied data.

Use INSERT to insert data into an existing table. INSERT specifies the following actions: 

If the destination table already exists, COPY inserts the copied data in the destination table.



If the destination table does not already exist, COPY reports an error and stops.

Use APPEND when you want to insert data in an existing table, or create a new table if the destination table does not exist. APPEND specifies the following actions: 

If the destination table already exists, COPY inserts the copied data in the destination table.



If the table does not already exist, COPY creates the table and then inserts the copied data in it.

Example B-1 Copying from a Remote Database to Your Local Database Using CREATE To copy HR from a remote database into a table called EMPLOYEE_COPY on your own database, enter the following command:

Note: See your DBA for an appropriate username, password, and service name for a remote computer that contains a copy of EMPLOYEE_COPY.

COPY FROM HR/your_password@BOSTONDB CREATE EMPCOPY USING SELECT * FROM HR

~Ramesh Raj

Oracle SQL*Plus COPY Command

Array fetch/bind size is 15. (arraysize is 15) Will commit when done. (copycommit is 0) Maximum long size is 80. (long is 80)

SQL*Plus then creates the table EMPLOYEE_COPY and copies the rows: Table SALESMAN created. 5 rows selected from HR@BOSTONDB. 5 rows inserted into SALESMAN. 5 rows committed into SALESMAN 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 HR, with the password your_password. 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. Since most COPY commands are longer than one line, you must use a line continuation hyphen (-), optionally preceded by a space, at the end of each line except the last.

Interpreting the Messages that COPY Displays The first three messages displayed by COPY show the values of SET command variables that affect the COPY operation. The most important one is LONG, which limits the length of a LONG column's value. (LONG is a datatype, similar to CHAR.) If the source table contains a LONG column, COPY truncates values in that column to the length specified by the system variable LONG. 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.) 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.

Specifying Another User's Table You can refer to another user's table in a COPY command by qualifying the table name with the username, just as you would in your local database, or in a query with a database link. For example, to make a local copy of a table named DEPARTMENT owned by the username ADAMS on the database associated with the Oracle Net connect identifier BOSTONDB, you would enter

~Ramesh Raj

Oracle SQL*Plus COPY Command COPY FROM HR/your_password@BOSTONDB CREATE EMPLOYEE_COPY2 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.

Copying Data between Tables on One Database You can copy data from one table to another in a single database (local or remote). To copy between tables in your local database, specify your own username and password and the service name for your local database in either a FROM or a TO clause (omit the other clause): COPY FROM HR/your_password@MYDATABASE INSERT EMPLOYEE_COPY2 USING SELECT * FROM EMPLOYEE_COPY

To copy between tables on a remote database, include the same username, password, and service name in the FROM and TO clauses: COPY FROM HR/your_password@BOSTONDB TO HR/your_password@BOSTONDB INSERT EMPLOYEE_COPY2 USING SELECT * FROM EMPLOYEE_COPY

~Ramesh Raj

Related Documents


More Documents from ""

Access Ays Issa
May 2020 12
Gimble
May 2020 11
Sample Booklet
April 2020 15
Yogesh1
May 2020 14