这当前正在按预期工作,除非我希望它不用序列中的下一个值填充acctnum,如果它为null。我的问题是在我的FROM FINAL TABLE中尝试下一个if语句时。它完全不喜欢它。这是一个语法问题,我用google搜索无济于事。
CREATE SEQUENCE acct_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO CYCLE
CACHE 24
;
SELECT accntnum AS new_acct, old_acct
FROM FINAL TABLE (
UPDATE accounts INCLUDE(old_acct INT)
SET accntnum = NEXT VALUE FOR acct_seq, old_acct = accntnum
)
ORDER BY old_acct;
我这样做的原因是我可以返回新旧账号。即使它们是空的,我仍然想要返回新的和旧的帐号。
答案 0 :(得分:1)
表面上可以通过在CASE
语句中使用UPDATE
表达式来完成,例如
...
UPDATE accounts INCLUDE(old_acct INT)
SET
accntnum = CASE WHEN accntnum IS NOT NULL THEN NEXT VALUE FOR acct_seq END,
...
但是,DB2不允许在CASE表达式中使用NEXT VALUE
。由于您只更新accntnum
中的非NULL值,因此可以在UPDATE
语句中明确指定,并在需要时为未修改的行返回NULL。
SELECT accntnum AS new_acct, old_acct
FROM FINAL TABLE (
UPDATE accounts INCLUDE(old_acct INT)
SET accntnum = NEXT VALUE FOR acct_seq, old_acct = accntnum
WHERE accntnum IS NOT NULL
)
UNION ALL
SELECT NULL as new_acct, NULL as old_acct
FROM accounts
WHERE accntnum IS NULL
ORDER BY old_acct;