I am trying to compile a .sqc file on AIX using DB2 getting sql error SQL0511N "The FOR UPDATE clause is not allowed because the table specified by the cursor cannot be modified.SQLSTATE=42829" while declaring a Cursor with host_variable_int on "FETCH FIRST :host_variable_int ROWS ONLY" but same statement is getting compiled when pass number instead of host_variable like "FETCH FIRST 40 ROWS ONLY".
ERROR SQL Statement:
EXEC SQL DECLARE cursor_name CURSOR WITH HOLD FOR SELECT a.coulmn_names FROM TABLE_NAME a WHERE a.TABLE_COLUMN_1 = :host_variable AND a.TABLE_COLUMN_2 IN ( SELECT b.coulmn_names FROM TABLE_NAME b WHERE b.TABLE_COLUMN_1 = :host_variable AND a.TABLE_COLUMN_3 = b.TABLE_COLUMN_3) FETCH FIRST **:host_variable_int** ROWS ONLY FOR UPDATE OF coulmn_name;
NON ERROR SQL STATEMENT:
EXEC SQL DECLARE cursor_name CURSOR WITH HOLD FOR SELECT a.coulmn_names FROM TABLE_NAME a WHERE a.TABLE_COLUMN_1 = :host_variable AND a.TABLE_COLUMN_2 IN ( SELECT b.coulmn_names FROM TABLE_NAME b WHERE b.TABLE_COLUMN_1 = :host_variable AND a.TABLE_COLUMN_3 = b.TABLE_COLUMN_3) FETCH FIRST **40** ROWS ONLY FOR UPDATE OF coulmn_name;
I am not sure what difference in the two cursor declaration statement Does someone have any idea about this? Thanks in advance
For current versions of Db2, this is the expected result. You cannot use a host-variable for the
fetch firstrow count.In Db2, the fetch-clause has this syntax for the fetch-row-count, according to the documentation.
If you want to vary the fetch first value , you have to use dynamic-SQL and you cannot use static-sql.