0

我正在从对象对表进行 jdbc 更新。该对象有几个属性,我只想用具有值的对象更新我的表。问题是如果它们不在对象中,下面的更新语句将擦除所有省略的值。

例如:

 String statementStr = "update users " 
                         + "set firstName = ?, lastName = ?, email = ?, birthday = ?, "
                         + "gender = ?"
                         + "where id = ?;";

如何修改此语句以仅更新对象中的填充值?

4

1 回答 1

1

如果我理解正确,那么您可以使用ISNULL()仅使用不可为空的属性更新字段,否则不更新该字段:

UPDATE users 
SET firstName = ISNULL(@nameParam, firstName), 
    lastName = ISNULL(@lastNameParam, lastName), 
    email = ISNULL(@emailParam, email),
    ...
WHERE id = ?;
于 2012-09-07T00:06:40.003 回答