How do I invoke a postgresql sequence while inserting new row into a table?
I want to do something like this:
insert into biz_term( biz_term_id, biz_term_name, ) values(SELECT nextval(idsequence)', 'temp' );
I want to do it because when I am trying to insert new record into biz_term
table then sequence idsequence
is not getting invoked directly. How to invoke it?
Use the INSERT INTO command with a grouped set of data to insert new values. Use the INSERT INTO command in conjunction with a SELECT statement to insert existing values from another table. Use the COPY (or \copy) command to insert values from a system file.
A sequence in PostgreSQL is a user-defined schema-bound object that yields a sequence of integers based on a specified specification. The CREATE SEQUENCE statement is used to create sequences in PostgreSQL. Now let's analyze the above syntax: First, set the name of the sequence after the CREATE SEQUENCE clause.
A sequence in PostgreSQL is a user-defined schema-bound object that generates a sequence of integers based on a specified specification. To create a sequence in PostgreSQL, you use the CREATE SEQUENCE statement.
You got it almost. You don't need the SELECT in there:
insert into biz_term( biz_term_id, biz_term_name, ) values( nextval('idsequence'), 'temp' );
Any reasons you did not specify the biz_term_id as serial
(or bigserial
) which handles that automatically for you?
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