Amadou Kone
Amadou Kone

Reputation: 956

Insert string as row in Postgresql

I have some strings in an audit table that describe an entire row that was deleted. Ie, they look like:

'(029e99b4-6ea2-46e4-a89d-91d96aff41c4/4/5b,029e99b4-6ea2-46e4-a89d-91d96aff41c4,029e99b4-6ea2-46e4-a89d-91d96aff41c4,M4,short_rainy,54,Coffee,1,,2,,,2015-11-09,2,,,,,2,,)'

They were creating using the ROW() function in the useful script described here.

Now, I want to insert them into the table they came from. It still has the same schema, so each column in the string should correspond to the correct column in the table. How can I do that?

My apologies if this has been asked already - I looked around and only found this question, which doesn't seem workable in my case because I have so many columns.

Thanks

Upvotes: 0

Views: 379

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125544

Cast it to the table type before inserting:

insert into t
values
    ((
        029e99b4-6ea2-46e4-a89d-91d96aff41c4/4/5b,
        029e99b4-6ea2-46e4-a89d-91d96aff41c4,
        029e99b4-6ea2-46e4-a89d-91d96aff41c4,
        M4,short_rainy,54,Coffee,1,,2,,,2015-11-09,2,,,,,2,,
    )::t)

Upvotes: 1

Related Questions