PostgreSQL : Inserting data into a table using Select query.

This is a small post describing the PostgreSQL query for inserting data into a table using Select Query, as I had to use it for solving some issue in one of my projects. So, the query is below :

INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', (SELECT title from FilmCode where id = 'T_601') , 
106, '1961-06-16', 'Drama');

So, what you have to basically do is to put the ‘Select’ query in round brackets () and execute the query [ The brackets are the main thing to watch out for, as, otherwise the query will give an error on execution.]. The required value will be fetched from the source table and inserted into the desired table.

Hope this helps 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s