I'm having difficulties creating a SP in which I pass in a name of a table and query the SYS2 library to find out if it has an auto-increment field. If it does I query for the max value of that field in the table and then alter the table so the next used value is that result plus 1. This is for use when migrating production data over to development.
I'm not sure if it is possible to use "Execute Immediate" as part of a cursor declaration. I'm still fairly new to db2 in general, never mind for IBM. So any assistance would be greatly appreciated. If "Execute Immediate" is not allowed in a cursor declaration, how would I go about doing this?
I'm getting the error on the Cursor declaration (line 10), but here is the exact error code I'm getting:
SQL State: 42601
Vendor Code: -199
Message: [SQL0199] Keyword IMMEDIATE not expected. Valid tokens: <END-OF-STATEMENT>. Cause . . . . . : The keyword IMMEDIATE was not expected here. A syntax error was detected at keyword IMMEDIATE. The partial list of valid tokens is <END-OF-STATEMENT>. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
And then finally here is my SP
/* Creating procedure DLLIB.SETNXTINC@ */
CREATE OR REPLACE PROCEDURE DLLIB.SETNXTINC@(IN TABLE CHARACTER (10) ) LANGUAGE SQL CONTAINS SQL PROGRAM TYPE SUB CONCURRENT ACCESS RESOLUTION DEFAULT DYNAMIC RESULT SETS 0 OLD SAVEPOINT LEVEL COMMIT ON RETURN NO
SET @STMT1 = 'SELECT COLUMN_NAME ' ||
'FROM QSYS2.SYSCOLUMNS ' ||
'WHERE TABLE_SCHEMA =''DLLIB'' and table_name = ''' || TRIM(TABLE) || '''' ||
'AND HAS_DEFAULT = ''I'' ' ||
'OR HAS_DEFAULT = ''J'';';
DECLARE cursor1 CURSOR FOR
EXECUTE IMMEDIATE @STMT1;
OPEN cursor1;
WHILE (sqlcode == 0){
FETCH cursor1 INTO field;
SET @STMT2 = 'ALTER TABLE DLLIB.' || TRIM(TABLE) || ''' ' ||
'ALTER COLUMN ' || TRIM(field) || ' RESTART WITH ( ' ||
'SELECT MAX(' || TRIM(field) || ') ' ||
'FROM DLLIB.' || TRIM(TABLE) || ');';
EXECUTE IMMEDIATE @STMT2;
};;
/* Setting label text for DLLIB.SETNXTINC@ */
LABEL ON ROUTINE DLLIB.SETNXTINC@ ( CHAR() ) IS 'Set the next auto-increment';
/* Setting comment text for DLLIB.SETNXTINC@ */
COMMENT ON PARAMETER ROUTINE DLLIB.SETNXTINC@ ( CHAR() ) (TABLE IS 'Table from DLLIB' ) ;