SQL文
postgreSQL
select
'select * from ' || tc.table_name || ' order by ' || string_agg(ccu.column_name, ',') || ';' sql
from
information_schema.table_constraints tc
, information_schema.constraint_column_usage ccu
where
tc.table_catalog = (データベース名)
and tc.table_schema = (スキーマ名)
and tc.constraint_type = 'PRIMARY KEY'
and tc.table_catalog = ccu.table_catalog
and tc.table_schema = ccu.table_schema
and tc.table_name = ccu.table_name
and tc.constraint_name = ccu.constraint_name
group by
tc.table_name
結果
テーブルごとにPKでソートするSQLを生成します。
select * from table1 order by column1, column2;
select * from table2 order by column1, column2;
:
: