Using query to set the column type in PostgreSQL
Asked Answered
I

3

2

After the excellent answer by Alexandre GUIDET, I attempted to run the following query:

 create table egg (id (SELECT 
  pg_catalog.format_type(a.atttypid, a.atttypmod) as Datatype 
  FROM 
  pg_catalog.pg_attribute a 
  WHERE 
    a.attnum > 0 
  AND NOT a.attisdropped 
  AND a.attrelid = ( 
    SELECT c.oid 
    FROM pg_catalog.pg_class c 
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace 
    WHERE c.relname ~ '^(TABLENAME)$' 
   AND pg_catalog.pg_table_is_visible(c.oid) 
  ) 
  and a.attname = 'COLUMNNAME'));

PostgreSQL, however, complains about incorrect syntax. Specifically it says that I cannot write: create table egg (id (SELECT.
Are there any workarounds? Can't I convert the result of a query to text and reuse it as a query?

Institutionalize answered 2/12, 2010 at 15:13 Comment(0)
V
4

There is a much simpler way to do that.

SELECT pg_typeof(col)::text FROM tbl LIMIT 1

Only precondition is that the template table holds at least one row. See the manual on pg_typeof()

As Milen wrote, you need to EXECUTE dynamic DDL statements like this.
A much simpler DO statement:

DO $$BEGIN
EXECUTE 'CREATE TABLE egg (id '
         || (SELECT pg_typeof(col)::text FROM tbl LIMIT 1) || ')';
END$$;

Or, if you are not sure the template table has any rows:

DO $$BEGIN
EXECUTE (
   SELECT format('CREATE TABLE egg (id %s)'
               , format_type(atttypid, atttypmod))
   FROM   pg_catalog.pg_attribute
   WHERE  attrelid = 'tbl'::regclass  -- name of template table
   AND    attname = 'col'             -- name of template column
   AND    attnum > 0 AND NOT attisdropped
   );
END$$;

These conditions seem redundant, since you look for a specific column any

format() requires Postgres 9.1+.

Related:

Vachel answered 26/10, 2011 at 8:59 Comment(0)
P
1

You can either convert that query to a function or (if you have Postgres 9.0) to an anonymous code block:

DO $$DECLARE the_type text;
BEGIN
    SELECT ... AS datatype INTO the_type FROM <the rest of your query>;
    EXECUTE 'create table egg ( id ' || the_type || <the rest of your create table statement>;
END$$;
Plurality answered 2/12, 2010 at 15:48 Comment(0)
F
0

You can either have a table a definition or a query, but not both. Maybe your thinking of the select into command.

Faradic answered 3/12, 2010 at 0:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.