title: Adding DB2 dynamic SQL code within an existing where clause
body: <p>I have a query which runs for orders placed in the last hour. Depending on the client, certain conditions are applied to  the WHERE clause of the query.  The SQL WHERE Clause is stored in a separate table for each client.</p>
  <p>The query would look like this:</p>
  <pre><code>SELECT * FROM ORDERS
  WHERE &lt;INSERT SQL STATEMEMT&gt;
  </code></pre>
  <p>I created a FUNCTION to extract the SQL Statement from the table.</p>
  <pre><code>CREATE OR REPLACE FUNCTION QUERY_SQL( 
  iCLIENTID VARCHAR(10)) 
  RETURNS VARCHAR(4000)
  LANGUAGE SQL 
  SPECIFIC FN_QUERY_SQL
  READS SQL DATA 
  NO EXTERNAL ACTION 
  DETERMINISTIC 
  BEGIN ATOMIC 
  
  DECLARE SQLQUERY VARCHAR(4000);
  
  SET SQLQUERY=(SELECT CAST(SQL_STMT AS VARCHAR(4000)) 
  FROM CONSUMER_NOTIFICATION 
              WHERE 
      CLIENT_ID=iCLIENTID
      FETCH FIRST ROW ONLY
      );
    RETURN SQLQUERY; 
  
    END
  </code></pre>
  <p>I'm having a problem just inserting the code into the WHERE clause. I've tried to PREPARE the SQL Code but that does not seem to work as the SQL is only a WHERE clause not a complete query.</p>
  <p>The DB2 dynamic SQL I've seen generates based on parameter values where the SQL state is fairly fixed.  I need to change the whole WHERE clause per client.</p>
  <p>Thanks for the help in advanced.</p>