title: Create dynamic tables based on a loop in PostgreSQL 9.2
body: <p>I have a function where I want to create a table for a every year based on the year from bill date which I will be looping.</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION ccdb.ccdb_archival()
    RETURNS void AS
  $BODY$
  DECLARE dpsql text;
  DECLARE i smallint;
  BEGIN
  
  FOR i IN SELECT DISTINCT EXTRACT(year FROM bill_date) FROM ccdb.bills ORDER  BY 1 LOOP
  
  DO $$ 
      BEGIN
      CREATE TABLE IF NOT EXISTS ccdb_archival.bills||i (LIKE ccdb.bills INCLUDING ALL);
      BEGIN
          ALTER TABLE ccdb_archival.bills ADD COLUMN archival_date timestamp;
      EXCEPTION
          WHEN duplicate_column THEN RAISE NOTICE 'column archival_date already exists in &lt;table_name&gt;.';
      END;
      END;
  $$; 
  
  INSERT INTO ccdb_archival.bills
  SELECT *, now() AS archival_date
  FROM ccdb.bills
  WHERE bill_date::date &gt;= current_date - interval '3 years' AND bill_date::date &lt; current_date - interval '8 years';
  END LOOP;
  END;
  $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;
  </code></pre>
  
  <p>I want to concatenate the year with the actual table name for each year.
  I am unable to do the same with the above code. I get an error:</p>
  
  <pre><code>ERROR:  syntax error at or near "||"
  LINE 3:   CREATE TABLE IF NOT EXISTS ccdb_archival.bills||i (LI...
  </code></pre>
  
  <p>Please suggest how do I achieve my requirement.</p>