Ich habe hier mal einen Versuch gemacht mit einer autarken SP ohne separates Reset des Generators. Da ich wenig Praxis mit
FB habe, bitte gut testen. Vielleicht schaut ja auch einer der
FB Kenner hier mal drüber.
SQL-Code:
-- create daily cyclic sequence number / expression
-- table storing last used date
-- drop table rndatebuffer;
create table rndatebuffer (
isRestricted char(1) Default 'X' unique, --force one record only
lastDate date not null ,
constraint forceOneRow check (restricted='X')); --force one record only
-- insert into rndatebuffer (lastdate) values(CURRENT_DATE-1);
-- select * from rndatebuffer;
-- sequence
CREATE GENERATOR gen_rn;
SET GENERATOR gen_rn TO 0;
--drop procedure getrn;
--procedure to fetch new "number"
SET TERM ^ ;
CREATE Procedure GetRN RETURNS (prn varchar(20)) as
declare aDif integer;
BEGIN
select CURRENT_DATE-lastdate from rndatebuffer into :aDif; -- detect date border
if (aDif is null) then -- init
begin
insert into rndatebuffer (lastdate) values(CURRENT_DATE);
execute statement 'set generator gen_rn to 0';
end
If (aDif <> 0) then -- date border reached
begin
update rndatebuffer set lastdate=CURRENT_DATE; -- reset date buffer
execute statement 'set generator gen_rn to 0'; -- reset generator
end
for
select -- build Rechnungsnummer
CAST(Extract(year FROM cast('NOW' as date))*10000 +
Extract(month FROM cast('NOW' as date))*100 +
Extract(day FROM cast('NOW' as date)) AS VARCHAR(8))||
LPAD (gen_id(gen_rn,1), 6, '0')
from RDB$DATABASE into :prn
do begin
suspend;
end
END
^
SET TERM ; ^
P.S.: der Aufruf wäre dann einfach