I want to be able to use the current value of an auto ID by giving it some kind of name and just referring to it (instead of calling SELECT for each record):
Requests table has a serial column named requestid
.
insert into testschema.requests (chid) values
(1);
I want to use select currval()
multiple times without having to use SELECT for each line:
insert into testschema.requestinfo (requestid, reportid, amount) values
((select currval('testschema.requests_requestid_seq')), 10, 40),
((select currval('testschema.requests_requestid_seq')), 15, 29),
((select currval('testschema.requests_requestid_seq')), 40, 4);
I've tried placing the select on its own line with no success:
SELECT currval('testschema.requests_requestid_seq') as reqid;
INSERT INTO testschema.requestinfo(requestid, reportid) VALUES
(reqid, 2);
Bottom line: I want to refer to the currval without having to use SELECT currval()
for each line.
Best Answer
First of all, you would not need a subquery in your statement. The bare function does the job:
Next,
currval()
only makes sense if you have (implicitly or explicitly) callednextval()
for the same sequence before in your session. If you have not callednextval()
for any other sequences in the meantime, you can simplify withlastval()
.Either way, if you have many rows like this, look to @dezso's answer for a way to avoid typing the function repeatedly.
currval()
andlastval()
are volatile functions:Result:
That means, they will still be evaluated for every row that way.
To also avoid repeated execution of the function, move it to a separate CTE or subquery and
CROSS JOIN
to your input values: