title: Drop table if it exists with DB2/400 SQL
body: <p>My goal is pretty straightforward - if table has rows, drop it.
  Despite the fact that currently there are several similar answers none of them worked for me.</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/8270937/db2-drop-table-if-exists-equivalent">DB2 Drop table if exists equivalent</a></li>
  </ol>
  
  <p>Suggested solution:</p>
  
  <pre><code>IF EXISTS (SELECT name FROM sysibm.systables WHERE name = 'mylib.mytable') THEN
  DROP TABLE mylib.mytable;END IF;
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>SQL State: 42601 Vendor Code: -199 Message: [SQL0199] Keyword IF not expected.
  Valid tokens: ( CL END GET SET CALL DROP FREE HOLD LOCK OPEN WITH ALTER BEGIN
  </code></pre>
  
  <ol start="2">
  <li><a href="https://stackoverflow.com/questions/29033226/drop-db2-table-if-exists">Drop DB2 table if exists</a></li>
  </ol>
  
  <p>Suggested solution:</p>
  
  <pre><code>--#SET TERMINATOR @
  begin
    declare statement varchar(128);
    declare continue handle for sqlstate '42710' BEGIN END;
    SET STATEMENT = 'DROP TABLE MYLIB.MYTABLE';
    EXECUTE IMMEDIATE STATEMENT;
  end @
  </code></pre>
  
  <p>Result: </p>
  
  <p><code>Message: [SQL0104] Token HANDLE was not invalid. Valid tokens: HANDLER</code> or, if replace <code>handle</code> with <code>handler</code>:</p>
  
  <pre><code>Message: [SQL0199] Keyword STATEMENT not expected. Valid tokens: SQL PATH RESULT SCHEMA CURRENT CONNECTION DESCRIPTOR.
  </code></pre>
  
  <ol start="3">
  <li>From <a href="https://stackoverflow.com/questions/2493055/drop-a-db2-view-if-it-exists">answer</a> about views</li>
  </ol>
  
  <p>Suggested solution:</p>
  
  <p><code>DROP TABLE MY_TABLE ONLY IF EXISTS</code> <a href="https://bytes.com/topic/db2/answers/509144-drop-table-view-only-if-exists" rel="nofollow noreferrer">source</a>.</p>
  
  <p>Result: </p>
  
  <p><code>Message: [SQL0104] Token ONLY was not invalid. Valid tokens: RESTRICT CASCADE</code></p>
  
  <p>So, I wonder if an alternate solution exists. <code>CL</code> solution is also interesting. </p>