0

我正在尝试将三个更新语句合并为一个。

"UPDATE DOT_WORKS SET START_DATE = :StartDate WHERE ID = :WorksId and END_DATE IS NULL;"
"UPDATE DOT_WORKS SET WORKS_TYPE = :WorksType WHERE ID = WorksId and WORKS_GROUP = :WorksGroup;"
"UPDATE DOT_WORKS SET WORKS_CONNECTION = :WorksConn WHERE ID = WorksId and WORKS_PLACE = :WorksPlace;"

我想知道是否有办法做到这一点。我尝试这样做的原因是将调用保存到数据库。调用 db 一次而不是调用三次更有效。

谢谢!

4

1 回答 1

2
UPDATE DOT_WORKS 
SET START_DATE = case when END_DATE IS NULL then :StartDate else START_DATE end,
    WORKS_TYPE = case when WORKS_GROUP = :WorksGroup then :WorksType else WORKS_TYPE end,
    WORKS_CONNECTION = case when WORKS_PLACE = :WorksPlace then :WorksConn else WORKS_CONNECTION end
WHERE ID = :WorksId 
and 
(
  END_DATE IS NULL OR
  WORKS_GROUP = :WorksGroup OR
  WORKS_PLACE = :WorksPlace
)
于 2015-01-08T09:42:13.780 回答