Anda di halaman 1dari 4

Copying Schemas DB2 Version 9.

1 lets you copy all of the objects in one schema to another schema using a new routine named ADMIN_COPY_SCHEMA. This procedure creates the target schema and copies objects (tables, views, indexes, and so on) from the source sc hema to the same table space or even to a different table space. The procedure a rguments also let you specify whether only the object definitions are to be copi ed, 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 objects exist in the source schema, information about them is added to an error table, b ut 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 the DDL and the data will be copied into the new schema. When moving the data DB2 u ses the load from cursor command. object_owner - this is the authorization ID that you would like to use as th e owner for the newly created objects. If you specify NULL then the new objects will 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 t he source schema target_tablespace - this is a list of tablespaces that you want to use to ho ld 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 is the nth tablespace in the source list then DB2 will use the nth tablespace in t he target_tablespace list to put this table in. So you need to match up the sour ce tablespaces with the list of target tablespaces that you want the objects mov ed to. error_tables_schema - this is the schema for the error table error_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 stored procedure and lives in the SYSTOOLSPACE tablespace and will contain one or more message in the event anything goes wrong when objects are being copied For example, the following code causes objects in a schema named copied to a new schema named "ROSITA". Any data in those objects ll. (The examples that follow require a connection to the SAMPLE u don't have the SAMPLE database created on your system, you can tering the db2sampl command from any command prompt.) list tables for schema rosita Table/View Schema Type Creation time ------------------------------- --------------- ----- -------------------------"MELNYK" to be is loaded as we database; if yo create it by en

0 record(s) selected. call admin_copy_schema ('MELNYK', 'ROSITA', 'COPYNO', 'MELNYK', 'USERSPACE1', 'USERSPACE1', 'MELNYK', 'SCHEMACOPYERR') Value of output parameters -------------------------Parameter Name : ERRORTABSCHEMA Parameter Value : MELNYK Parameter Name : ERRORTABNAME Parameter Value : SCHEMACOPYERR Return Status = 0 list tables for schema rosita Table/View ------------------------------ACT ADEFUSR CL_SCHED DEPARTMENT DEPT EMP EMP_ACT EMP_PHOTO EMP_RESUME EMPACT EMPLOYEE EMPMDC EMPPROJACT IN_TRAY ORG PROJ PROJACT PROJECT SALES STAFF VACT VASTRDE1 VASTRDE2 VDEPMG1 VDEPT VEMP VEMPDPT1 VEMPLP VEMPPROJACT VFORPLA VHDEPT VPHONE VPROJ VPROJACT VPROJRE1 VPSTRDE1 Type ----T S T T A A A T T A T T T T T A T T T T V V V V V V V V V V V V V V V V

VPSTRDE2 VSTAFAC1 VSTAFAC2 39 record(s) selected. select * from rosita.org DEPTNUMB -------10 15 20 38 42 51 66 84

V V V

DEPTNAME MANAGER DIVISION LOCATION -------------- ------- ---------- ------------Head Office 160 Corporate New York New England 50 Eastern Boston Mid Atlantic 10 Eastern Washington South Atlantic 30 Eastern Atlanta Great Lakes 100 Midwest Chicago Plains 140 Midwest Dallas Pacific 270 Western San Francisco Mountain 290 Western Denver

8 record(s) selected. You can also use the db2move utility to copy one or more schemas from a source d atabase to a target database. The source and the target database must be differe nt, 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 use d or routines are called, is needed by the db2move (COPY) utility to store some intermediate operation tables. Suppose you have a database named SRCDB with one table named CUSTOMERS. create table customers ( custno integer, custname varchar(12) ); insert insert insert insert insert into into into into into customers customers customers customers customers values values values values values (12, (18, (22, (23, (30, 'Smith'); 'Lamarr'); 'Piepiorka'); 'Tallerico'); 'Wong');

You can use the following db2move command to copy the schema "MELNYK" (which inc ludes the CUSTOMERS table) in SRCDB to a different database named TARDB. The def ault copy mode is DDL_AND_LOAD, which creates supported objects from the source schema in the target database, and populates target tables with data from tables in 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;

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.msg LOADTABLE.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 SQL authorization ID Local database alias list tables for user Table/View Schema Type ----------------------------- --------------- ----CUSTOMERS MELNYK T 1 record(s) selected. select * from customers CUSTNO ----------12 18 22 23 30 CUSTNAME -----------Smith Lamarr Piepiorka Tallerico Wong = DB2/NT 9.1.2 = MELNYK = TARDB

5 record(s) selected. connect reset DB20000I The SQL command completed successfully.

Anda mungkin juga menyukai