title: Postgres generate sql output using data dictionary tables
body: <p>All I need is to get a SQL query output as :</p>
  
  <pre><code>ALTER TABLE TABLE_NAME
  ADD CONSTRAINT
  FOREIGN KEY (COLUMN_NAME)
     REFERENCES (PARENT_TABLE_NAME);
  </code></pre>
  
  <p>I'm running the below DYNAMIC query USING DATA DICTIONARY TABLES,</p>
  
  <pre><code>SELECT DISTINCT
     'ALTER TABLE  ' || cs.TABLE_NAME ||
     'ADD CONSTRAINT' || rc.CONSTRAINT_NAME ||
     'FOREIGN KEY' || c.COLUMN_NAME ||
     'REFERENCES' || cs.TABLE_NAME ||
     ' (' || cs.CONSTRAINT_NAME || ') ' ||
     ' ON UPDATE ' || rc.UPDATE_RULE ||
     ' ON DELETE ' || rc.DELETE_RULE
  FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,
     INFORMATION_SCHEMA.TABLE_CONSTRAINTS CS, 
     INFORMATION_SCHEMA.COLUMNS C
  WHERE cs.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
    AND cs.TABLE_NAME = c.TABLE_NAME 
    AND UPPER(cs.TABLE_SCHEMA) = 'SSP2_PCAT';
  </code></pre>
  
  <p>But here even though I'm able to generate the desired output, the concern is its not giving the <code>PARENT_TABLE_NAME</code> here, 
  rather its giving the same table_name after the <code>ALTER TABLE</code> Keywords. </p>
  
  <p>I hope this is clear as we are using Dynamic SQL here and any help is absolutely appreciated!</p>