title: In DB2 PL/SQL anonymous block declare local variables and declare continue handler results in error?
body: <p>I am trying to make a simple script that handles errors from statements that are ok to fail gracefully. This is for DB2 v10.5 (Linux)</p>
  
  <p>Example create a table and handle if table already exists</p>
  
  <pre><code>begin 
      DECLARE CONTINUE HANDLER FOR SQLSTATE '42710' BEGIN END; 
      EXECUTE IMMEDIATE 'CREATE TABLE T(c1 INT)'; 
  end
  /
  </code></pre>
  
  <p>This works ok</p>
  
  <p>Now I had a slightly more complex block that originally used local variables and I was trying to added the DECLARE CONTINUE HANDLER and I could not get it to compile. Simplifying the problem I found that by adding a declare statement before the begin fails if I have the Handler line (note I am not using the localVariable here, but it fails just by adding that statement in. If I run same statement without DECLARE CONTINUE HANDLER, it also runs ok</p>
  
  <pre><code>declare 
    localVariable  INT;
  begin 
      DECLARE CONTINUE HANDLER FOR SQLSTATE '42710' BEGIN END; 
      EXECUTE IMMEDIATE 'CREATE TABLE T(c1 INT)'; 
  end
  /
  </code></pre>
  
  <p>Resulting error code</p>
  
  <blockquote>
    <p>Error report: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601,
    SQLERRMC=end;end;BEGIN, DRIVER=3.68.61</p>
  </blockquote>