0

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' ) ;
d.lanza38
  • 2,525
  • 7
  • 30
  • 52

1 Answers1

0

First off, you don't need to dynamically prepare you first statement.

Secondly, you can't use a SELECT in the RESTART WITH, you'll have to use 2 statements

Thirdly, if you use VARCHAR instead of CHAR, you don't need to use any TRIM()s

Lastly, using TABLE as a parameter name is bad practice as it is a reserved word.

You want something like so

CREATE OR REPLACE PROCEDURE QGPL.SETNXTINC@(IN MYTABLE VARCHAR (128) ) 
LANGUAGE SQL 
MODIFIES SQL DATA
PROGRAM TYPE SUB 
CONCURRENT ACCESS RESOLUTION DEFAULT 
DYNAMIC RESULT SETS 0 
OLD SAVEPOINT LEVEL 
COMMIT ON RETURN NO 

BEGIN
declare mycolumn varchar(128);
declare stmt2 varchar(1000);
declare stmt3 varchar(1000);
declare mymaxvalue integer;

-- Table known at runtime, a static statement is all we need
SELECT COLUMN_NAME INTO mycolumn
FROM QSYS2.SYSCOLUMNS 
WHERE TABLE_SCHEMA = 'DLLIB'
  AND TABLE_NAME = mytable
  AND HAS_DEFAULT = 'I'
  OR HAS_DEFAULT = 'J';

-- Need to use a dynamic statement here 
-- as the affected table is not known till runtime
-- need VALUES INTO as SELECT INTO can not be used dynamically
SET STMT2 = 'VALUES (SELECT MAX(' || mycolumn || ') ' || 
    'FROM DLLIB.' || mytable || ')' || 'INTO ?';

PREPARE S2 from stmt2;
EXECUTE S2 using mymaxvalue;

-- we want to restart with a value 1 more than the current max
SET mymaxvalue = mymaxvalue + 1;

-- Need to use a dynamic statement here 
-- as the affected table is not known till runtime
SET STMT3 = 'ALTER TABLE DLLIB.' || mytable || ' ALTER COLUMN ' 
            || mycolumn || ' RESTART WITH ' || char(mymaxvalue);
EXECUTE IMMEDIATE STMT3; 
END;

One more thing to consider, you might want to LOCK the table in exclusive mode prior to running STMT2; otherwise there's a possibility that a record with a higher value got added between the execution of STMT2 and STMT3.

Charles
  • 21,637
  • 1
  • 20
  • 44
  • Thank you, it worked perfectly. I was only expecting details on what I was doing wrong and not the complete code. But I supposed I was doing a lot wrong, this helps a lot, thank you. – d.lanza38 Mar 27 '15 at 18:55
  • Glad I could help. I actually have done the same task in the past. But I used RPG along with system API to determine the column name. So it was interesting to compare to the pure SQL solution. – Charles Mar 28 '15 at 22:57