DB2: How to get generated always as statement to work with session user

天涯浪子 提交于 2019-12-25 04:20:06

问题


I need to get a userstamp into a table and have not managed to figure out how the GENERATED FOR EACH ROW ON UPDATE AS statement works with the SESSION_USER variable in DB2 10.5 (LUW).

Managed to get an implementation working using a function which has a fake variable for forcing the evaluation in update statements:

CREATE OR REPLACE FUNCTION XXX.CURRENT_USER( tmp varchar(128))
    SPECIFIC xxx.XXX_CURRENT_USER
RETURNS VARCHAR(128) 
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION 
BEGIN
  RETURN session_user ;
END
GO

CREATE TABLE xxx (
    i INTEGER,
    t VARCHAR(128) GENERATED ALWAYS AS (XXX.CURRENT_USER(i))
)

However, would be nice have less "hacky" implementation for a basic thing like this.

For the time stamps there is that "FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP" statement but no equivalent for other register variables it seems.

Help is very much appreciated


回答1:


Does this work?

CREATE TABLE xxx (
    i INTEGER,
    t VARCHAR(128) WITH DEFAULT session_user
);

I don't have DB2 on hand to check, but this is very similar to the syntax used in other databases (although the more typical syntax does not use WITH).



来源:https://stackoverflow.com/questions/39685389/db2-how-to-get-generated-always-as-statement-to-work-with-session-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!