0

I have a user-defined function that modifies sql data. It inserts into one table and returns the ID of the returned row. Trying to use it like this

UPDATE table_X SET REV = REVISION(:timestamp, :user) WHERE ...conditions; 

DB2 gives me the error: Routine "REDACTED.REVISION" (specific name "REDACTED") is defined with the MODIFIES SQL DATA option, which is not valid in the context where the routine is invoked.

IBM documentation for this error specifically says this is how a function with MODIFIES SQL DATA is supposed to be used.

Is this just a bug in DB2 or am I missing something?

1 Answer 1

2

If you have DB2 for LUW (and not DB2 for Z/OS which your link refers to), then you should follow correct documentation.
Refer to the CREATE FUNCTION (SQL scalar, table, or row) statement link with the following note about the MODIFIES SQL DATA clause:

Valid only for compiled scalar function definition and an inlined table function definition. A compiled scalar function defined as MODIFIES SQL DATA can only be used as the only element on the right side of an assignment statement that is within a compound SQL (compiled) statement.

This means, that it can't be used in the UPDATE statement, for example. Only in the SET statement of a compiled (BEGIN ... END, not BEGIN ATOMIC ... END) compound statement.

CREATE OR REPLACE FUNCTION TEST_MOD() RETURNS INT MODIFIES SQL DATA BEGIN RETURN 1; END DB20000I The SQL command completed successfully. CREATE TABLE IF NOT EXISTS TEST_MOD(I INT) DB20000I The SQL command completed successfully. BEGIN DECLARE V_RES INT; SET V_RES = TEST_MOD(); END DB20000I The SQL command completed successfully. BEGIN UPDATE TEST_MOD SET I = TEST_MOD(); END DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0740N Routine "DB2ADMIN.TEST_MOD" (specific name "SQL250127142445606") is defined with the MODIFIES SQL DATA option, which is not valid in the context where the routine is invoked. LINE NUMBER=2. SQLSTATE=51034 BEGIN ATOMIC DECLARE V_RES INT; SET V_RES = TEST_MOD(); END DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0740N Routine "DB2ADMIN.TEST_MOD" (specific name "SQL250127142445606") is defined with the MODIFIES SQL DATA option, which is not valid in the context where the routine is invoked. LINE NUMBER=3. SQLSTATE=51034 
2
  • Thank you. The error codes list only seems to be available in the z/os documentation for whatever reason. Commented Jan 27 at 12:19
  • 2
    @coladict The same info is available in the DB2 for LUW docs as well. SQL messages. The SQLCODE = -740 description is available at the SQL0000-0999 link with quite a descriptive explanation. The same explanation can be obtained with DB2 CLP, for example: db2 "? sql740". Commented Jan 27 at 13:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.