This write-up shows how to insert multiple rows into a Postgres table with the help of several examples. PostgreSQL provides a returning clause that can be used with the INSERT query that returns the currently inserted rows. Finally, specify a VALUES keyword followed by the list of values to be inserted in multiple rows. Next, specify a column or list of columns in the parentheses. To do that, use the INSERT INTO query followed by the table name into which you want to insert the rows. To insert multiple rows in a table, the comma-separated syntax is used. The output verified that the ORDER BY clause sorted the rows in ascending order (based on article_id). Let’s run the following command to sort the rows in ascending order: SELECT * FROM article_details However, the ORDER BY clause can be used to sort the table’s data in a specific order. ![]() PostgreSQL maintains the insertion order. Let’s execute the below command to see the updated table: SELECT * FROM article_details The output shows that the RETURNING clause returns the newly inserted rows. Postgres provides an optional clause named RETURNING that can be used with the INSERT statement to return the currently inserted data: INSERT INTO The output clarifies that five rows have been inserted into the targeted table successfully.Įxample #2: How to Insert and Return Multiple Rows in PostgreSQL? You can check/verify the inserted data by executing the below-given query: SElECT * FROM article_details Let’s run the INSERT INTO statement to insert five new rows to the selected table: INSERT INTOĪrticle_details(article_id, article_title, published_date) The output shows that the article_details table has three columns: article_id, article_title, and published_date. We have already created a table named article_details whose details are as follows: SELECT * FROM article_details
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |