Another no-magic-here post, but quite a useful one when working with data in dev, and needing to massage it.
Have you found the need to duplicate a row, only with slight modification? SQL doesn't have a standard way to do it, and while some rdbms accept some sort of "SELECT * EXCEPT(id) FROM xyz", you might not have it available in your db (postgres)
The clearest solution I found is here:
It's all about creating a temporary table, and use "like" and "insert into", to avoid having to write all the fields:
CREATE TEMP TABLE tmp (like web_book);
INSERT INTO tmp SELECT * FROM web_book WHERE id = 3;
UPDATE tmp SET id = nextval('web_book_id_seq');
INSERT INTO web_book SELECT * from tmp;
In fact, we could simplify it further (at least in postgres) with:
CREATE TEMP TABLE tmp as SELECT * FROM web_book WHERE id = 3;
UPDATE tmp SET id = nextval('web_book_id_seq');
INSERT INTO web_book SELECT * from tmp;
No hay comentarios:
Publicar un comentario