diff --git a/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/SqlServerDBManager.java b/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/SqlServerDBManager.java index fcd341dc3..2d8b0b2fe 100644 --- a/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/SqlServerDBManager.java +++ b/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/SqlServerDBManager.java @@ -162,17 +162,26 @@ public void connectDatabase(Connection connection, String database) { @Override public void copyTable(Connection connection, String databaseName, String schemaName, String tableName, String newTableName, boolean copyData) throws SQLException { - String sourceTable = buildFullTableName(databaseName, schemaName, tableName); - String targetTable = buildFullTableName(databaseName, schemaName, newTableName); - String sql; + String ddl = Chat2DBContext.getDbMetaData().tableDDL(connection, + new TableMetadataRequest(databaseName, schemaName, tableName)); + String createDdl = ddl.replace("[" + tableName + "]", "[" + newTableName + "]"); + log.info("copy table DDL: {}", createDdl); + DefaultSQLExecutor.getInstance().execute(connection, createDdl, resultSet -> null); + if (copyData) { - sql = String.format(SQL_COPY_TABLE_DATA, targetTable, sourceTable); - } else { - sql = String.format(SQL_COPY_TABLE_STRUCTURE, targetTable, sourceTable); + String sourceTable = buildFullTableName(databaseName, schemaName, tableName); + String targetTable = buildFullTableName(databaseName, schemaName, newTableName); + String identityOn = String.format(SQL_SET_IDENTITY_INSERT, targetTable, "ON"); + String identityOff = String.format(SQL_SET_IDENTITY_INSERT, targetTable, "OFF"); + String insertSql = String.format(SQL_COPY_TABLE_DATA, targetTable, sourceTable); + log.info("copy table data sql: {}", insertSql); + try { + DefaultSQLExecutor.getInstance().execute(connection, identityOn, resultSet -> null); + DefaultSQLExecutor.getInstance().execute(connection, insertSql, resultSet -> null); + } finally { + DefaultSQLExecutor.getInstance().execute(connection, identityOff, resultSet -> null); + } } - - log.info(" copy table sql : {}", sql); - DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> null); } diff --git a/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/constant/SqlServerDBManagerConstants.java b/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/constant/SqlServerDBManagerConstants.java index 2a3f74d1f..9e0b4c577 100644 --- a/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/constant/SqlServerDBManagerConstants.java +++ b/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/constant/SqlServerDBManagerConstants.java @@ -22,8 +22,8 @@ public final class SqlServerDBManagerConstants { public static final String SQL_DROP_TABLE_EXISTS = "DROP TABLE IF EXISTS "; public static final String SQL_DROP_VIEW_EXISTS = "DROP VIEW IF EXISTS "; - public static final String SQL_COPY_TABLE_DATA = "SELECT * INTO %s FROM %s"; - public static final String SQL_COPY_TABLE_STRUCTURE = "SELECT * INTO %s FROM %s WHERE 1=0"; + public static final String SQL_COPY_TABLE_DATA = "INSERT INTO %s SELECT * FROM %s"; + public static final String SQL_SET_IDENTITY_INSERT = "SET IDENTITY_INSERT %s %s"; public static final String SQL_DROP_TABLE = "DROP TABLE %s"; public static final String SQL_DROP_VIEW = "DROP VIEW %s"; public static final String SQL_USE_DATABASE = "use [%s];";