public void convertSqlFile(File file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String outPath = file.getAbsolutePath().replace(".", "_converted.");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath), "utf-8"));
String line;
while ((line = br.readLine()) != null) {
// 跳過 MySQL 專用指令
if (line.contains("CREATE DATABASE") || line.contains("USE `")) continue;
// 移除反引號
if (line.toLowerCase().contains(") values(")) {
int idx = line.toLowerCase().indexOf("values(");
String prefix = line.substring(0, idx).replace("`", "");
line = prefix + line.substring(idx);
}
bw.write(line + "\r\n");
}
br.close(); bw.close();
}
INSERT,避免 SQL Server 解析器堆疊溢位sqlcmd -i file.sql 批次執行詳細步驟待補充...
| 項目 | MySQL | SQL Server 2008 | 處理方式 |
|---|---|---|---|
| 外鍵約束 | ON DELETE RESTRICT | 不支援 | 移除或改用觸發器 |
| 建表判斷 | DROP TABLE IF EXISTS | 不支援 | 改用 IF OBJECT_ID() IS NOT NULL DROP TABLE |
| BLOB 類型 | BLOB/TEXT | IMAGE/TEXT/NTEXT | 類型映射轉換 |
| 自增 | AUTO_INCREMENT | IDENTITY(1,1) | 語法轉換 |
| 字符集 | CHARSET=utf8 | COLLATE Chinese_PRC_CI_AS | 指定校對規則 |
BULK INSERT 或 bcp,再建索引/約束