Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

Removing all columns with given name

$
0
0

I am converting a MSSQL schema to PostgreSQL and in that schema most tables have a column called Timestamp that are of MSSQL timestamp datatype which is effectively rowversion.

When inserting records into those tables you do not need to specify values for timestamp columns as MSSQL auto updates that column. I believe this is the same as xmin in PG.

So now when my app tries to insert into the table PG blows up saying it needs a value for the Timestamp column.

I’m therefore thinking after importing the schema writing something that drops all columns from my tables that are called Timestamp and have timestamp datatype.

I have found I can get column and table info from pgclass and pgattribute however I”m now stuck how to loop and do checks and then drop columns. Could someone point me in the right direction please?

select
    t.relname,
    a.attname,
    d.typname
from
  pg_class t
  INNER JOIN pg_attribute a 
  on a.attrelid = t.oid
  INNER JOIN pg_type d
  on d.oid = a.atttypid
  where relkind='r' and attname = 'timestamp' and d.typname = 'timestamp'
ORDER BY t.relname

Viewing all articles
Browse latest Browse all 1138

Latest Images

Trending Articles



Latest Images