How to create a function in DB2 that returns the value of a sequence?

こ雲淡風輕ζ 提交于 2019-12-06 02:02:34

问题


How to create a function in DB2 that obtains a value from a sequence and returns it?

It should be possible to use that function in select or insert statement, e.g:

select my_func() from xxx
insert into xxx values(my_func())

Basically I am using the sequence value in a complex formula, and I'd like to encapsulate the calculation inside a function.

Edit: I am not asking how to simply get next value from sequence.


回答1:


CREATE FUNCTION "MYSCHEMA"."MY_FUNC"(PARAM1 VARCHAR(4000))
     RETURNS INT
SPECIFIC SQL110520140321900 BEGIN ATOMIC
     DECLARE VAR1 INT;
     DECLARE VAR2 INT;
     SET VAR1  = NEXTVAL FOR MY_SEQ;
     SET VAR2 = VAR1 + 2000; --or whatever magic you want to do
     RETURN VAR2;
END

To try it out:

SELECT MY_FUNC('aa') FROM SYSIBM.SYSDUMMY1;


来源:https://stackoverflow.com/questions/5992308/how-to-create-a-function-in-db2-that-returns-the-value-of-a-sequence

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