title: How get information from multiple tables using cursor?
body: <p>I have a query, that returns multiple tables,  something like that:</p>
  
  <pre><code>SELECT TableName, DatabaseName +'.'+ TableName, ColumnName
  FROM DBC.Columns
  WHERE ColumnName = 'id'
  </code></pre>
  
  <p>And I need to loop through these tables by looking to the information stored in these tables, in order to get only specific tables.</p>
  
  <p>I tried something like code below, using 'LOOP' and cursor, but it says that <code>Query is invalid</code> (code have been taken from <a href="http://www.dwhpro.com/teradata-sql-stored-procedures-cursors/" rel="nofollow">here</a>):</p>
  
  <pre><code>DECLARE cursor_Tables CURSOR FOR     
      SELECT DatabaseName || '.' || TableName
      FROM   DBC.Columns
      WHERE  ColumnName  ='id'; 
  
  OPEN cursor_Tables; 
      label1: 
      LOOP    
          FETCH  cursor_Tables into tbName;
          IF (SQLSTATE ='02000') THEN
              LEAVE label1;
          END IF;
  
          CASE WHEN (  
              SELECT COUNT(*)
              FROM prd3_db_tmd.K_PTY_NK01
              WHERE id = 0 ) &gt; 0
               THEN tbName
          END 
      END LOOP label1;
  CLOSE cursor_Tables;
  END;
  </code></pre>
  
  <p>How can I actually deal with this problem? Do I need to use procedure in addition? DBMS is Teradata</p>