检测UPDATE语句中更改的SQL列

时间:2021-04-09 07:57:38

I have the following Java code:

我有以下Java代码:

PreparedStatement statement = conn.prepareStatement(updateString);
statement.execute();

I know that after that is executed, doing statement.getUpdateCount(); returns the number of updated rows. But, is there a way to know WHICH columns where updated?

我知道在执行之后,执行statement.getUpdateCount();返回更新的行数。但是,有没有办法知道更新的WHICH列?

The problem is that the code as it is, always passes the whole set of column values, even if some of those values did not change.

问题是代码原样,总是传递整个列值集,即使其中一些值没有改变。

I want to be able to tell whether a particular column, for example "NAME", changed or not, as a result of the underlying prepared UPDATE statement.

我希望能够通过基础准备的UPDATE语句判断特定列(例如“NAME”)是否发生了更改。

EDIT: I understand that it is possible to do a SELECT for that column, get current value, and compare with incoming new value to detect if it will change. However, it implies another trip to the database. This question is to determine whether it is possible or not through some JDBC/SQL communication mechanism just after invoking that statement.

编辑:我知道可以为该列执行SELECT,获取当前值,并与传入的新值进行比较以检测它是否会更改。但是,它意味着另一次访问数据库。这个问题是在调用该语句之后通过某种JDBC / SQL通信机制确定是否可能。

1 个解决方案

#1


0  

if you're just wholesale updating every column, regardless of whether the new value is different than the old value, then you'll probably have to programmatically select and check each column value prior to update.

如果您只是批量更新每一列,无论新值是否与旧值不同,那么您可能必须以编程方式选择并在更新之前检查每个列值。

so the pseudo code would be something like:

所以伪代码会是这样的:

select * from table where condition
execute
foreach row in result
  foreach column in row
    if (column != newColumn) document.write(column != newColumn);

PreparedStatement statement = conn.prepareStatement(updateString);
statement.execute();

#1


0  

if you're just wholesale updating every column, regardless of whether the new value is different than the old value, then you'll probably have to programmatically select and check each column value prior to update.

如果您只是批量更新每一列,无论新值是否与旧值不同,那么您可能必须以编程方式选择并在更新之前检查每个列值。

so the pseudo code would be something like:

所以伪代码会是这样的:

select * from table where condition
execute
foreach row in result
  foreach column in row
    if (column != newColumn) document.write(column != newColumn);

PreparedStatement statement = conn.prepareStatement(updateString);
statement.execute();