title: Dynamic cursors and WHERE clause Teradata
body: <p>I'm trying to write a procedure where the databasename is dynamic and is taken from a where clause.
  So far I have got this:</p>
  
  <pre><code>CREATE PROCEDURE Test
  
  (IN DBName VARCHAR(100), OUT RowCount DEC(10,2))
  
  BEGIN
  
  DECLARE SqlStr VARCHAR(1000);
  
  DECLARE C1 CURSOR FOR S1;
  
  SET SqlStr = 'SELECT count(*) FROM '|| DBNAME || '.MyTable ';
  
  PREPARE S1 FROM SqlStr;
  
  OPEN C1 USING DBName;
  
  FETCH C1 INTO RowCount;
  
  CLOSE C1;
  
  END;
  </code></pre>
  
  <p>I would need to add something like this now:</p>
  
  <pre><code>WHERE DBName = (SELECT 'firstpart||EnvName||' FROM EnvTable 
  WHERE EnvName = (SELECT EnvName FROM EnvTable WHERE Flag = 1 AND Priority = (SELECT MIN(Priority) FROM EnvTable))
  </code></pre>
  
  <p>Any ideas? Can I add this when I call the procedure?</p>