0

JDBC sql server:设置 IDENTITY_INSERT ON:无效。

PreparedStatement stmt = connection.prepareStatement("SET IDENTITY_INSERT Table_FOO ON");
stmt.execute();
stmt.close();


PreparedStatement stmt2 = connection.prepareStatement("insert into Table_FOO (id, name) values (100, 'abc')");
stmt2.execute();
stmt2.close();

错误:仍然抱怨 DENTITY_INSERT 为 OFF。

com.microsoft.sqlserver.jdbc.SQLServerException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“Table_FOO”中插入标识列的显式值。

4

1 回答 1

0

您可以尝试将语句组合成 1 个字符串并用分号终止语句

PreparedStatement stmt = connection.prepareStatement("SET IDENTITY_INSERT Table_FOO ON; " +
                                                     "insert into Table_FOO (id, name) values (100, 'abc');" +
                                                     "SET IDENTITY_INSERT Table_FOO OFF;");
stmt.execute();
stmt.close();
于 2020-09-08T02:25:03.723 回答