-1

i am doing some inserts with some of my tables and all fields but a few need to be inserted or updated. Like here. Is there a quick way to get the columns of a table returned as a string so i can copy them into my sql query? For example i have a table:

create table myTable1
(
column1                   integer  not null,
column2                   integer  not null ,
column3                   integer  not null,
column4                   integer  not null,
column5                   integer  not null,
...a lot more fields...
)

And later i want to insert something from another table myTable2 that has the same fields (dont question my ways and why i have two identical tables).

INSERT INTO myTable SELECT column1, column2, column3, column4, ...
FROM table_source

But the tables have so many fields it is cumbersome to write them donw manualy and it would be faster to have a string where i can just delete the column names i dont need. Is there a nice query that ouputs "column1, column2, column3, column4, ..." so i dont have to write that myself and can copy tha into my query?

HrkBrkkl
  • 613
  • 5
  • 22

1 Answers1

2

Found the answer quickly.

SELECT table_catalog, string_agg(column_name, ', ')
  FROM information_schema.columns
 WHERE table_schema = 'mySchema'
   AND table_name   = 'myTable' GROUP  BY 1;

This query does the trick for me.

HrkBrkkl
  • 613
  • 5
  • 22