I'd like to write a Postgres Stored Proc (cannot be a function) that accepts two args: seq_name and current ID.
If the ID has a value then the SP should return the value. If the ID is NULL then the code should call the sequence name passed in as arg and return that nextval.
I'd then like an example of calling that within PGAdmin. I thin it's do/begin/call but I don't know how to get that returned ID and display it.
Hope that makes sense. Tx
Was thinking it would be something like this:
CREATE OR REPLACE PROCEDURE sp_generate_seq ( s_seq_name VARCHAR(255), i_id INOUT INT )
LANGUAGE plpgsql
AS $$
BEGIN
CASE id_gen
WHEN i_id IS NULL THEN
SELECT nextval.s_seq_name
INTO i_id ;
END id_gen
END $$
do
$$
declare i_val int ;
begin
call sp_generate_seq ( 'foo_seq', i_val); -- Should return next sequence value since i_val is NULL
raise notice '%', i_val ;
-- I hope that calling again should return the same value since nextval will not be called
call sp_generate_seq ( 'foo_seq', i_val);
raise notice '%', i_val ;
end
$$