Question

I've got a table with the following:

Columns:
---
URL [nullable, e.g. "https://dba.stackexchange.com/"]
APPFK [nullable, e.g. 654654]
LINKTYPE ["Page", "Link", or "App"]

Check Constraints:
---
(URL IS NULL AND LINKTYPE <> "Link") OR (URL IS NOT NULL AND LINKTYPE = "Link")
(APPFK IS NULL AND LINKTYPE <> "App") OR (APPFK IS NOT NULL AND LINKTYPE = "App")

Since LinkType is calculable based on the nullability of URL and APPFK, I figured this was a perfect opportunity for a computed/generated/virtual column.

So, I tried to run:

alter table MYLIB.MYTABLE
add column testcol VARCHAR(4) generated always as
(
    CASE
        WHEN URL IS NOT NULL THEN 'LINK'
        WHEN APPFK IS NOT NULL THEN 'APP'
        ELSE 'PAGE'
    END
)

Annnd DB2 spat this back at me:

SQL State: 42601 Vendor Code: -104 Message: [SQL0104] Token ( was not valid. Valid tokens: IDENTITY. Cause . . . . . : A syntax error was detected at token (. Token ( is not a valid token. A partial list of valid tokens is IDENTITY. This list assumes that the statement is correct up to the token. The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : Do one or more of the following and try the request again: -- Verify the SQL statement in the area of the token (. Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. -- If the error token is , correct the SQL statement because it does not end with a valid clause.

I tried running the first example in this DB2 documentation, but it gave me the same error.

What's going wrong, here? Am I doing something wrong? Is the documentation wrong? Is it just because our DB2 is outdated (we're running Version 7, Revision 1), and if so is there a workaround?

Was it helpful?

Solution

Db2 versions for different platforms tend to have different features and statement syntax details, so you should always consult documentation for your exact database version and platform. If you do so, you will see that you can only use a limited selection of expressions for generated columns -- either an identity or a row change timestamp.

Instead of a generated column I suggest you simply create a view with your CASE expression for an extra column.

OTHER TIPS

What platform and version of Db2?

The documentation you linked to is for Db2 for Linux/Unix/Windows (aka LUW).

The db2-midrange tag is for Db2 for IBM i, whose CREATE TABLE documentation shows that only the following columns can be generated..

  • identity
  • row change timestamp
  • row-begin
  • row-end column
  • transaction-start-ID
  • generated expression

Note: The last four are relatively new and were basically added to support temporal tables in the most recent version of Db2 for IBM i (7.4)

That last one might seem like what you're trying but digging deeper, an "generated-expression" is one of the following "non-deterministic-expression"

  • DATA CHANGE OPERATION (flag indicating Insert/Update/Delete)
  • special-register
  • built-in-global-variable
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top