Home > OS >  Call a sequence through a function in PostgreSql
Call a sequence through a function in PostgreSql

Time:01-03

I have a sequence in the database, and I wanted to call the sequence through a function. I have tried below, need an assistance to create the function in a standard process, as this is not working.

select nextval('code_seq')
CREATE FUNCTION code_sequence(integer) RETURNS integer
    LANGUAGE SQL
    IMMUTABLE
    return select nextval('code_seq');

CodePudding user response:

CREATE FUNCTION local_accounts_sequence(integer) RETURNS integer
    LANGUAGE SQL
    IMMUTABLE
    RETURN (select nextval('code_seq'));
    
select local_accounts_sequence(1)   

enter image description here check out this solution

CodePudding user response:

I am able to achieve using below statements,

CREATE FUNCTION code_sequence(out seq int) 
    LANGUAGE plpgsql
    as $$ 
    begin
    select nextval('code_seq') into seq;
end;$$

CodePudding user response:

try this :

CREATE FUNCTION local_accounts_sequence() RETURNS integer   
    AS 'SELECT nextval(''code_seq'');'
    LANGUAGE SQL;

call it like this :

select local_accounts_sequence();
  • Related