I have a table, call it t1, with three integer columns c1, c2, c3. c1 has a default value of:
not null default nextval
For the INSERT statements I am currently doing, I want c2 to have the same value as will be assigned to c1. This is not the case for most of my inserts so it does not make sense to define c2 to have a default value or to have a trigger on update. Currently I am doing two statements:
INSERT INTO t1 (c3) VALUES (val3);
UPDATE t1 SET c2 = c1 WHERE //Get correct row
There is no guarantee in which order elements of a set will be processed. There is also no need to make two function calls. Use a sub-select or a CTE:
INSERT INTO t (c1, c2, c3)
SELECT x.s, x.s, val3
FROM (SELECT nextval('c1_seq') AS s) x;
Or with a CTE:
WITH x(s) AS (SELECT nextval('c1_seq'))
INSERT INTO t (c1, c2, c3)
SELECT x.s, x.s, val3
FROM x;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With