IBM DB2 数据复制迁移方法

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

背景:需要更改数据库表空间,或者需要将数据库中所有表的数据迁移到一个新的数据库中。步骤:1.通过db2控制台(db2cc)选中源数据库中的所有表,将其导出成DDL脚本;2.根据需要对脚本进行必要的修改,譬如更改表空间为GATHER;3.新建数据库,新建DMS表空间:GATHER;4.将DDL脚本在此数据库中执行;5.编写代码查询源数据库中的所有表,自动生成export脚本;6.编写代码查询源数据库中的所有表,自动生成import脚本;7.连接源数据库执行export脚本;8.连接目标数据库执行import脚本;附录1:生成export脚本代码示例:/**

* 创建导出脚本

* @param conn

* @param creator表创建者

* @param filePath

*/

* 创建导出脚本

* @param conn

* @param creator表创建者

* @param filePath

public void createExportFile(Connection conn,String creator,String filePath) throws Exception {

DBBase dbBase = new DBBase(conn);

String selectTableSql = "select name from sysibm.systables where creator = '" + creator + "' and type='T'";

try {

dbBase.executeQuery(selectTableSql);

} catch (Exception ex) {

throw ex;

} finally {

dbBase.close();

}

DBResult result = dbBase.getSelectDBResult();

List list = new ArrayList();

while (result.next()) {

String table = result.getString(1);

list.add(table);

}

StringBuffer sb = new StringBuffer();

String enterFlag = "

";

for (int i = 0; i < list.size();i++) {

String tableName = (String)list.get(i);

sb.append("db2 "export to aa" + String.valueOf(i+1)+ ".ixf of ixf select * from " + tableName + """);

sb.append(enterFlag);

}

String str = sb.toString();

FileUtility.saveStringToFile(filePath, str, false);

}附录2:生成import脚本代码示例/**

* 创建装载脚本

* @param conn

* @param creator表创建者

* @param filePath

*/

public void createLoadFile(Connection conn,String creator,String filePath) throws Exception {

DBBase dbBase = new DBBase(conn);

String selectTableSql = "select name from sysibm.systables where creator = '" + creator + "' and type='T'";

try {

dbBase.executeQuery(selectTableSql);

} catch (Exception ex) {

throw ex;

} finally {

dbBase.close();

}

DBResult result = dbBase.getSelectDBResult();

List list = new ArrayList();

while (result.next()) {

String table = result.getString(1);

list.add(table);

}

StringBuffer sb = new StringBuffer();

String enterFlag = "

";

for (int i = 0; i < list.size();i++) {

String tableName = (String)list.get(i);

sb.append("db2 "load from aa" + String.valueOf(i+1)+ ".ixf of ixf into " + tableName + " COPY NO without prompting "");

sb.append(enterFlag);

}

String str = sb.toString();

FileUtility.saveStringToFile(filePath, str, false);

}附录3:export脚本示例db2connecttotestdbusertestpasswordtest

db2"exporttoaa1.ixfofixfselect*fromtable1"

db2"exporttoaa2.ixfofixfselect*fromtable2"

db2connectreset附录4:import脚本示例db2connecttotestdbusertestpasswordtest

db2"loadfromaa1.ixfofixfreplaceintotable1COPYNOwithoutprompting"

db2"loadfromaa2.ixfofixfreplaceintotable2COPYNOwithoutprompting"

db2connectreset

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航