用于更新表但多列的 DB2 案例语句
Posted
技术标签:
【中文标题】用于更新表但多列的 DB2 案例语句【英文标题】:DB2 case statement for updating a table but multiple columns 【发布时间】:2021-06-28 04:34:43 【问题描述】:我正在尝试将 4 个单独的更新语句合并为一个更新语句。 我没有案例陈述的经验,或者我尝试做的事情是否可行。
关于如何构建它的任何意见? 这是我的尝试,但它不起作用。
UPDATE URL
SET CONTENT_TITLE =
CASE
WHEN ID = ? AND SYSTEM_UPDATE_REASON = ? AND CONTENT_TITLE IS NOT NULL AND ? NOT BETWEEN 400 AND 599 OR ID = ? AND SYSTEM_UPDATE_REASON = ? AND CONTENT_TITLE != '' AND ? NOT BETWEEN 400 AND 599 THEN ?
WHEN ID = ? AND CONTENT_TITLE IS NULL AND ? NOT BETWEEN 400 AND 599 OR ID = ? AND CONTENT_TITLE = '' AND ? NOT BETWEEN 400 AND 599 THEN ?
WHEN ID = ? AND CONTENT_TITLE IS NOT NULL AND ? BETWEEN 400 AND 599 THEN ?
WHEN ID = ? AND CONTENT_TITLE IS NULL AND ? BETWEEN 400 AND 599 OR ID = ? AND CONTENT_TITLE = '' AND ? BETWEEN 400 AND 599 THEN ?
SET CONTENT_DSCR =
CASE
WHEN ID = ? AND CONTENT_DSCR IS NULL AND ? NOT BETWEEN 400 AND 599 OR ID = ? AND CONTENT_DSCR = '' AND ? NOT BETWEEN 400 AND 599 THEN ?
WHEN ID = ? AND CONTENT_TITLE IS NOT NULL AND ? BETWEEN 400 AND 599 THEN ?
SET SYSTEM_UPDATE_REASON = 'title and dscr update'
SET UPDATED_ON = SYSDATE
END
这是我正在使用的占位符 (?):
u.urlId, 'title and dscr update', u.statusCode, u.urlId, 'title and dscr update',
u.statusCode, u.contentTitle, u.urlId, u.statusCode, u.urlId, u.statusCode,
u.contentTitle, u.urlId, u.statusCode, null, null, u.urlId, u.statusCode, u.urlId,
u.statusCode, u.contentDescription, u.urlId, u.statusCode, 'title and dscr update'
【问题讨论】:
【参考方案1】:也许它根本没有帮助,但是 - 让我试试(因为 小时 没有回复):
我不知道 DB2;如果它的 SQL 与 Oracle 的类似,那么
CASE
应该有它的END
整个UPDATE
语句中只有一个SET
关键字
逻辑上,当有OR
时,你应该使用括号(否则你会得到意想不到的结果)
类似这样的:
UPDATE URL
SET CONTENT_TITLE =
CASE
WHEN (ID = ? AND SYSTEM_UPDATE_REASON = ? AND CONTENT_TITLE IS NOT NULL AND ? NOT BETWEEN 400 AND 599)
OR (ID = ? AND SYSTEM_UPDATE_REASON = ? AND CONTENT_TITLE != '' AND ? NOT BETWEEN 400 AND 599) THEN ?
WHEN (ID = ? AND CONTENT_TITLE IS NULL AND ? NOT BETWEEN 400 AND 599)
OR (ID = ? AND CONTENT_TITLE = '' AND ? NOT BETWEEN 400 AND 599) THEN ?
WHEN ID = ? AND CONTENT_TITLE IS NOT NULL AND ? BETWEEN 400 AND 599 THEN ?
WHEN (ID = ? AND CONTENT_TITLE IS NULL AND ? BETWEEN 400 AND 599)
OR (ID = ? AND CONTENT_TITLE = '' AND ? BETWEEN 400 AND 599) THEN ?
END,
CONTENT_DSCR =
CASE
WHEN (ID = ? AND CONTENT_DSCR IS NULL AND ? NOT BETWEEN 400 AND 599)
OR (ID = ? AND CONTENT_DSCR = '' AND ? NOT BETWEEN 400 AND 599) THEN ?
WHEN ID = ? AND CONTENT_TITLE IS NOT NULL AND ? BETWEEN 400 AND 599 THEN ?
END,
SYSTEM_UPDATE_REASON = 'title and dscr update',
UPDATED_ON = SYSDATE
【讨论】:
我收到此错误:未处理该语句,因为该语句包含对以下之一的无效使用:无类型参数标记、DEFAULT 关键字或空值。以上是关于用于更新表但多列的 DB2 案例语句的主要内容,如果未能解决你的问题,请参考以下文章