getting back id generated via autoincrement

Moderator: NorbertKrupa

Post Reply
akina86
Newbie
Newbie
Posts: 1
Joined: Wed Mar 09, 2016 4:48 pm

getting back id generated via autoincrement

Post by akina86 » Wed Mar 09, 2016 4:52 pm

Hi

I have a table that is set to auto increment for the PK. Is there a command I can use so that I can get the generated auto increment key right after I insert?

Thanks!

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1800
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: getting back id generated via autoincrement

Post by JimKnicely » Wed Mar 09, 2016 9:06 pm

Check out the LAST_INSERT_ID function!

https://my.vertica.com/docs/7.2.x/HTML/ ... ERT_ID.htm

Example:

Code: Select all

dbadmin=> create table earth (wind auto_increment, fire varchar(20));
CREATE TABLE
dbadmin=> insert into earth (fire) values ('Disco Lives!');
 OUTPUT
--------
      1
(1 row)

dbadmin=> select * from earth;
 wind |     fire
------+--------------
    1 | Disco Lives!
(1 row)

dbadmin=> select last_insert_id();
 last_insert_id
----------------
              1
(1 row)
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “New to Vertica SQL”