title: Teradata stored procedure with dynamic parameters
body: <p>I'm trying to create a dunamic procedure that, given a specific dbname and tablename, creates a set of select statements using all column names available for that table.</p>
  
  <p>I'm using the following code:</p>
  
  <pre><code>REPLACE PROCEDURE sum_col 
  ( 
  IN TABLENAME VARCHAR(50), DBNAME VARCHAR(286)
  )
  
  BEGIN
  
  DECLARE SqlTxt VARCHAR(3000);
  
  FOR cur AS
  
        SELECT
  
           TRIM(a.DatabaseName) AS DBNAME,
           TRIM(a.TABLENAME) AS TABLENAME,
           TRIM(b.ColumnName) AS ColumnName
  
        FROM dbc.Tables a
        INNER JOIN dbc.COLUMNS AS b
        ON a.DatabaseName=b.DatabaseName AND a.TABLENAME = b.TABLENAME
  
        WHERE a.DatabaseName = :DBNAME
        AND a.TABLENAME = :TABLENAME
              AND TableKind = 'T'
  
  
  DO 
  SET SqlTxt =  'SELECT ' || '''' || TRIM(cur.ColumnName) || '''' ||  ', CASE
                             WHEN SUM(CNT) IS NULL THEN 0
                             ELSE SUM(CNT)
                             END AS CntNull
  
  FROM (
  SELECT  0 AS cnt    
  FROM    ' || TRIM(cur.DBNAME) || '.'  || TRIM(cur.TABLENAME)  ||' )a ;';
  
  CALL dbc.sysexecsql(:SqlTxt);
  
     END FOR;
  
  END;
  </code></pre>
  
  <p>But when I call the statement I get this error:
  CALL Failed.  [5568] SUM_COL:SQL statement is not supported within a stored procedure.</p>
  
  <p>How can I fix this?</p>