Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Is it possible to select from all tables inside the schema? I got all the table names from

select table_name from information_schema.tables

but I am unable to use it to do my query.

share|improve this question
    
You must define exactly what to select. You cannot retrieve incompatible row structures in a single select. –  Erwin Brandstetter Dec 23 '14 at 18:10

2 Answers 2

You can't do it directly as one can not write a 'normal' query if the table names are not known (ie. coming from some variable or subquery). But you can build and execute a dynamic SQL for this. For example, if you need the column 'name' from every table, you can do the following:

FOR i IN SELECT table_name 
           FROM information_schema.tables 
          WHERE table_schema = 'your_desired_schema'
LOOP
    sql_string := sql_string || format($$
        -- some whitespace is mandatory here
        UNION
        SELECT name FROM %I
    $$,
    i.table_name);
END LOOP;

EXECUTE sql_string;

In this form it won't work however, since you cannot SELECT in plpqsql unless you do it into a variable. You can either create a temporary table for this, loop over the results (in an other FOR loop), or - not using UNION - just return in every iteration, depending on your needs.

And, of course, this presumes that you want to select a single column (or more than one, but always with the same name and type) from all the tables. If you simply need all the data from every table, then the tables must have the same structure (the same column types in the same order, with the same names).

Note that the format() function was introduced in version 9.1.

share|improve this answer

This is a workaround based on the above suggested approach.

 DECLARE
 tables CURSOR FOR SELECT * 
      FROM information_schema.tables 
      WHERE table_schema = 'public' 
      ORDER BY "table_name" ASC
      LIMIT ((SELECT count(*)
          FROM information_schema.tables
          WHERE table_schema = 'public')-1);
          --Because the following prepared string starts with a 'UNION ALL',
          --this completes the query string with a select starting with the last table
          sql_string text := 'SELECT field1, field7, field8, "source" FROM ' || quote_IDENT((SELECT "table_name" FROM information_schema.tables WHERE table_schema = 'public' ORDER BY "table_name" DESC LIMIT 1));

 BEGIN

 FOR table_record IN tables LOOP
     sql_string := sql_string || '
         UNION ALL
         SELECT columns FROM ' || quote_IDENT(table_record."table_name");
 END LOOP;

 sql_string := sql_string||';';
 RETURN sql_string;
 --EXECUTE sql_string;

 END;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.