Created
November 28, 2023 16:58
-
-
Save zacharyarnaise/05cb8ba79fb28ab5e950e12fb9393f4c to your computer and use it in GitHub Desktop.
PostgreSQL: duplicate a row multiple times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TEMPORARY TABLE temporary_table AS SELECT * FROM <my_table> WHERE <condition>; | |
| -- Remove any columns that will cause a constraint error when re-inserting the row | |
| ALTER TABLE temporary_table DROP COLUMN id; | |
| -- Re-insert 100 copies of the row with a new UUID | |
| INSERT INTO <my_table> SELECT uuid_generate_v4() AS id, temporary_table.* FROM temporary_table CROSS JOIN GENERATE_SERIES(1,100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment