title: Db2 stored procedure to increment a column value for a certain unique combination
body: <p>Db2 stored procedure to increment a column value for a certain unique combination. </p>
  
  <p>I want to write a DB2 stored procedure where <code>SEQ_I</code> is a column value that should only be incremented for a single combination of the column values (<code>SCHOOL_I, DEPT_I</code>, and <code>LIST_I</code>). </p>
  
  <p>In my below procedure, I want to use the <code>max(SEQ_I) + 1</code> for school, dept, and list combo</p>
  
  <p><code>SCHOOL_I, DEPT_I, LIST_I</code> and <code>SEQ_I</code> are columns of a table <code>SCHOOL_DEPT</code></p>
  
  <p>I tried writing it the below way. Please guide. </p>
  
  <pre><code>CREATE PROCEDURE CREATE_PCT_OFF_SUPPLIER  
          (IN IN_SCHOOL_I INTEGER
          ,IN IN_DEPT_I CHAR(6)
          ,IN IN_LIST_I INTEGER
          ,IN IN_SEQ_I SMALLINT)
  P1: BEGIN
          SELECT COUNT(*) AS COMB FROM SCHOOL_DEPT
          WHERE SCHOOL_I= IN_SCHOOL_I AND DEPT_I=IN_DEPT_I AND LIST_I = IN_LIST_I;
  
          IF COMB = 1 THEN  
          INSERT INTO SCHOOL_DEPT(SCHOOL_I,DEPT_I, LIST_I,SEQ_I) VALUES (IN_SCHOOL_I, IN_DEPT_I, _IN_LIST_I, MAX(IN_SEQ_I)+1); 
          ELSE
          INSERT INTO SCHOOL_DEPT(SCHOOL_I,DEPT_I, LIST_I) VALUES (IN_SCHOOL_I, IN_DEPT_I, _IN_LIST_I); 
  END P1
  </code></pre>