CREATE TABLE TBDB_IMPORTACAO (
    ID INTEGER NOT NULL,
    DESCRICAO VARCHAR(100),
    TABELA BLOB SUB_TYPE 1 SEGMENT SIZE 80);

ALTER TABLE TBDB_IMPORTACAO
ADD CONSTRAINT PK_TBDB_IMPORTACAO
PRIMARY KEY (ID);

CREATE SEQUENCE GTBDB_IMPORTACAO;

SET TERM ^ ;

create trigger ttbdb_importacao_b01i for tbdb_importacao
active before insert position 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gtbdb_importacao,1);
end
^

SET TERM ; ^

UPDATE OR INSERT INTO TBMENU (ID, CONTROLE, GRUPO, SUBGRUPO, TIPO, URL, DESCRICAO, DATAHORA)
                      VALUES (474, 11800, 'ADM', 'ADM', '1   ', '', 'IMPORTAÇÃO DE REGISTROS', '2021-02-23 13:41:24')
                    MATCHING (ID);


SET TERM ^ ;

create or alter procedure SP_UPDATE_GENERATOR
as
declare variable GENERATOR_NAME varchar(200);
declare variable POS integer;
begin

  for
    select
        RDB$GENERATOR_NAME
    from RDB$GENERATORS
  into
    :GENERATOR_NAME
  do begin

    in autonomous transaction do
    begin
       execute statement 'select max(id) from ' || replace(:GENERATOR_NAME,'GTB','TB') into :POS;
       execute statement 'ALTER SEQUENCE '||:GENERATOR_NAME||' RESTART WITH '||:POS||' INCREMENT BY 1;';
    end

    in autonomous transaction do
    begin
       execute statement 'select max(codigo) from ' || replace(:GENERATOR_NAME,'GTB','TB') into :POS;
       execute statement 'ALTER SEQUENCE '||:GENERATOR_NAME||' RESTART WITH '||:POS||' INCREMENT BY 1;';
    end

  end

end
^

SET TERM ; ^

