select/display last inserted serial id in postgres

后端 未结 3 1183
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 06:21

in sql server I do like this:

insert into foo(name) values(\'bob\')
select @@identity;

so I get a query/scalar result displayed

how to

3条回答
  •  清歌不尽
    2021-02-09 07:11

    It would be

    GET DIAGNOSTICS YourParam = RESULT_OID;

    See here http://www.postgresql.org/docs/8.2/static/plpgsql-statements.html scroll down to 37.6.6. Obtaining the Result Status

    asker edit: I tried this:

    create or replace function aaa() returns int as $$ 
    declare 
    a int;
    begin
    insert into oameni values(default, 'aaa');
    get diagnostics a = result_oid;
    return a;
    end;
    $$ language plpgsql;
    

    it always returns 0, you know what's wrong here?

提交回复
热议问题