title: For loop cursor in teradata
body: <p>In my Teradata Stored Procedure, I want to have a for loop cursor against a dynamic sql.</p>
  
  <p>Below is the code snippet </p>
  
  <pre><code>SET get_exclude_condition = '';
  
  SET colum_id = 'SELECT MIN (parent_criteria_id) ,MAX (parent_criteria_id)  FROM  arc_mdm_tbls.intnl_mtch_criteria WHERE act_ind = 1 AND criteria_typ = ''Exclude'' AND mtch_technique_id ='||mtch_technique_id||';' ;
  PREPARE input_stmt FROM colum_id;
  OPEN flex_cursor;
  FETCH flex_cursor INTO parent_criteria_id_min , parent_criteria_id_max ;
  CLOSE flex_cursor;
  
  SET get_exclude_condition = '';
  
  WHILE (parent_criteria_id_min &lt;=  parent_criteria_id_max)
  DO
  
      SET get_exclude_condition = get_exclude_condition || '(  ';
  
      SET for_loop_stmt = 'SELECT criteria  FROM arc_mdm_tbls.intnl_mtch_criteria WHERE act_ind = 1 AND mtch_technique_id ='||mtch_technique_id||' AND criteria_typ= ''Exclude'' AND parent_criteria_id ='||parent_criteria_id_min||';';
  
      FOR for_loop_rule AS c_cursor_rule CURSOR FOR 
          for_loop_stmt
      DO
  </code></pre>
  
  <p>Can I declare a for loop cursor like this ?
  Or do I need to have something like this only ?</p>
  
  <pre><code>FOR for_loop_rule AS c_cursor_rule CURSOR FOR 
          SELECT rule_id 
          FROM arc_stage_tbls.assmt_scoring_rules 
          WHERE rule_typ = :v_RuleType
          ORDER BY rule_id
  DO  
  </code></pre>
  
  <p>I mean can I first frame the dynamic sql and then have a for loop cursor on top of that or with the cursor declaration only I need to have a static sql query ?</p>
  
  <p>Please clarify. </p>