這是我從MS SQLServer轉換到PostgreSQL的存儲過程。有沒有更好的方式在Postgres中編寫它。另外什麼應該是最佳做法?任何幫助將不勝感激。編寫PostgreSQl函數的更好方法
create or replace function GenSerialNo_SP1(tname character varying) returns AS $$
--declare @PK bigint
declare totalRec bigint;
declare getRec bigint;
Begin
--set @PK = 1
set totalRec=-1;
for getRec inselect coalesce(primary_key,0) from STD_SERIAL_NOS
where table_name = tname
loop
open getRec;
fetch next from getRec into totalRec;
close getRec;
deallocate getRec;
end loop;
if totalRec = -1 then
insert into STD_SERIAL_NOS (TABLE_NAME, PRIMARY_KEY) values (tname, 1);
else
update STD_SERIAL_NOS set primary_key = primary_key +1
where table_name = tname;
end if;
end;
$$ language plpgsql;
你到底想達到什麼目的?你爲什麼不使用'SEQUENCE'?你在尋找無間隙序列嗎? –