Copying Schemas

Embed Size (px)

Citation preview

  • 7/30/2019 Copying Schemas

    1/4

    Copying Schemas

    DB2 Version 9.1 lets you copy all of the objects in one schema to another schemausing a new routine named ADMIN_COPY_SCHEMA. This procedure creates the targetschema and copies objects (tables, views, indexes, and so on) from the source schema to the same table space or even to a different table space. The procedure arguments also let you specify whether only the object definitions are to be copied, or whether any existing data in those objects is to be copied as well.

    Some objects, such as packages or typed tables, are not copied. If such objectsexist in the source schema, information about them is added to an error table, but the other (valid) objects in the schema are copied successfully.

    The syntax of the ADMIN_COPY_SCHEMA routine is as follows:

    ADMIN_COPY_SCHEMA ('source_schema', 'target_schema','copy_mode', 'object_owner', 'source_tablespace','target_tablespace', 'error_table_schema','error_table_name')

    where

    source_schema - is the name of the schema you want to copy objects from.target_schema - is the target schema name you want to copy objects to.copymode - is either 'DDL', in which case the stored procedure will copy all

    of the objects but not the data in the objects, or COPY, in which case both theDDL and the data will be copied into the new schema. When moving the data DB2 uses the load from cursor command.

    object_owner - this is the authorization ID that you would like to use as the owner for the newly created objects. If you specify NULL then the new objectswill be owned by the userid that is running the copy schema command.

    source_tablespace - this is a list of the tablespaces that hold objects in the source schema

    target_tablespace - this is a list of tablespaces that you want to use to hold the target schemas objects. When a table is being copied, DB2 looks in the so

    urce tablespace list to see what tablespace this object is coming from. If it isthe nth tablespace in the source list then DB2 will use the nth tablespace in the target_tablespace list to put this table in. So you need to match up the source tablespaces with the list of target tablespaces that you want the objects moved to.

    error_tables_schema - this is the schema for the error tableerror_table_name - this is a table that will hold error messages that may be

    produced by the copy schema. This table is automatically created by the storedprocedure and lives in the SYSTOOLSPACE tablespace and will contain one or moremessage in the event anything goes wrong when objects are being copied

    For example, the following code causes objects in a schema named "MELNYK" to be

    copied to a new schema named "ROSITA". Any data in those objects is loaded as well. (The examples that follow require a connection to the SAMPLE database; if you don't have the SAMPLE database created on your system, you can create it by entering the db2sampl command from any command prompt.)

    list tables for schema rosita

    Table/View Schema Type Creation time------------------------------- --------------- ----- --------------------------

  • 7/30/2019 Copying Schemas

    2/4

    0 record(s) selected.

    call admin_copy_schema ('MELNYK', 'ROSITA','COPYNO', 'MELNYK', 'USERSPACE1','USERSPACE1', 'MELNYK','SCHEMACOPYERR')

    Value of output parameters--------------------------Parameter Name : ERRORTABSCHEMAParameter Value : MELNYK

    Parameter Name : ERRORTABNAMEParameter Value : SCHEMACOPYERR

    Return Status = 0

    list tables for schema rosita

    Table/View Type ------------------------------- -----

    ACT TADEFUSR SCL_SCHED TDEPARTMENT TDEPT AEMP AEMP_ACT AEMP_PHOTO TEMP_RESUME TEMPACT AEMPLOYEE TEMPMDC TEMPPROJACT T

    IN_TRAY TORG TPROJ APROJACT TPROJECT TSALES TSTAFF TVACT VVASTRDE1 VVASTRDE2 VVDEPMG1 VVDEPT VVEMP V

    VEMPDPT1 VVEMPLP VVEMPPROJACT VVFORPLA VVHDEPT VVPHONE VVPROJ VVPROJACT VVPROJRE1 VVPSTRDE1 V

  • 7/30/2019 Copying Schemas

    3/4

    VPSTRDE2 VVSTAFAC1 VVSTAFAC2 V

    39 record(s) selected.

    select * from rosita.org

    DEPTNUMB DEPTNAME MANAGER DIVISION LOCATION-------- -------------- ------- ---------- -------------

    10 Head Office 160 Corporate New York15 New England 50 Eastern Boston20 Mid Atlantic 10 Eastern Washington38 South Atlantic 30 Eastern Atlanta42 Great Lakes 100 Midwest Chicago51 Plains 140 Midwest Dallas66 Pacific 270 Western San Francisco84 Mountain 290 Western Denver

    8 record(s) selected.

    You can also use the db2move utility to copy one or more schemas from a source database to a target database. The source and the target database must be different, and a table space named SYSTOOLSPACE must already exist; this table space, w

    hich is created by the system the first time that certain built-in tools are used or routines are called, is needed by the db2move (COPY) utility to store someintermediate operation tables.

    Suppose you have a database named SRCDB with one table named CUSTOMERS.

    create table customers (custno integer,custname varchar(12));

    insert into customers values (12, 'Smith');insert into customers values (18, 'Lamarr');

    insert into customers values (22, 'Piepiorka');insert into customers values (23, 'Tallerico');insert into customers values (30, 'Wong');

    You can use the following db2move command to copy the schema "MELNYK" (which includes the CUSTOMERS table) in SRCDB to a different database named TARDB. The default copy mode is DDL_AND_LOAD, which creates supported objects from the sourceschema in the target database, and populates target tables with data from tablesin the source schema.

    db2move srcdb copy -sn MELNYK -co TARGET_DB tardb USER melnyk USING mypaswd2

    Application code page not determined, using ANSI codepage 1252

    ***** DB2MOVE *****

    Action: COPY

    Start time: Sun Feb 25 12:47:21 2007

    All schema names matching: MELNYK;

  • 7/30/2019 Copying Schemas

    4/4

    Connecting to database SRCDB ... successful! Server : DB2 Common Server V9.1.2

    Copy schema MELNYK to MELNYK on the target database TARDB

    Create DMT : "SYSTOOLS"."DMT_45e1cbab6fc62"

    Start Load Phase :

    db2move finished successfully

    Files generated:-----------------COPYSCHEMA.20070225124721.msgLOADTABLE.20070225124721.MSG

    Please delete these files when they are no longer needed.

    End time: Sun Feb 25 12:47:29 2007

    Finally, you can verify that both the schema and the data were copied correctly:

    connect to tardb

    Database Connection Information

    Database server = DB2/NT 9.1.2SQL authorization ID = MELNYKLocal database alias = TARDB

    list tables for user

    Table/View Schema Type----------------------------- --------------- -----CUSTOMERS MELNYK T

    1 record(s) selected.

    select * from customers

    CUSTNO CUSTNAME----------- ------------

    12 Smith18 Lamarr22 Piepiorka23 Tallerico30 Wong

    5 record(s) selected.

    connect resetDB20000I The SQL command completed successfully.