需求:變更表空間或將全庫資料遷移至新資料庫。採用 Export → Load 方案,並以 Java 程式自動產生腳本,減少人工錯誤。
GATHER)。SYSCAT.TABLES 產生 Export 腳本。public void createExportFile(Connection conn, String creator, String filePath) throws Exception {
DBBase dbBase = new DBBase(conn);
String sql = "SELECT name FROM sysibm.systables WHERE creator = '" + creator + "' AND type = 'T'";
dbBase.executeQuery(sql);
DBResult result = dbBase.getSelectDBResult();
List tables = new ArrayList<>();
while (result.next()) tables.add(result.getString(1));
dbBase.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tables.size(); i++) {
sb.append("db2 \"export to aa").append(i+1).append(".ixf of ixf select * from ")
.append(tables.get(i)).append("\"\r\n");
}
FileUtility.saveStringToFile(filePath, sb.toString(), false);
}
public void createLoadFile(Connection conn, String creator, String filePath) throws Exception {
DBBase dbBase = new DBBase(conn);
String sql = "SELECT name FROM sysibm.systables WHERE creator = '" + creator + "' AND type = 'T'";
dbBase.executeQuery(sql);
DBResult result = dbBase.getSelectDBResult();
List tables = new ArrayList<>();
while (result.next()) tables.add(result.getString(1));
dbBase.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tables.size(); i++) {
sb.append("db2 \"load from aa").append(i+1).append(".ixf of ixf replace into ")
.append(tables.get(i)).append(" COPY NO without prompting\"\r\n");
}
FileUtility.saveStringToFile(filePath, sb.toString(), false);
}
db2 connect to testdb user test password test
db2 "export to aa1.ixf of ixf select * from table1"
db2 "export to aa2.ixf of ixf select * from table2"
db2 connect reset
db2 connect to testdb user test password test
db2 "load from aa1.ixf of ixf replace into table1 COPY NO without prompting"
db2 "load from aa2.ixf of ixf replace into table2 COPY NO without prompting"
db2 connect reset
NONRECOVERABLE + COPY NO 最大化速度,事後立即完整備份。db2move 工具簡化全庫操作。