Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke sequence while inserting new record into postgresql table?

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?

like image 933
Rajesh Avatar asked Aug 08 '12 12:08

Rajesh


People also ask

What PostgreSQL commands would you use to insert a new record into table viewing?

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.

Can we create sequence in PostgreSQL?

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.

Does Postgres support sequence?

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.


1 Answers

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?

like image 188
a_horse_with_no_name Avatar answered Sep 19 '22 21:09

a_horse_with_no_name