Page 1 of 1

getting back id generated via autoincrement

Posted: Wed Mar 09, 2016 4:52 pm
by akina86
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!

Re: getting back id generated via autoincrement

Posted: Wed Mar 09, 2016 9:06 pm
by JimKnicely
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)