econz icon

Firebird - Gera PK, Generator e Trigger ao criar tabela

econz | PRO | 07/17/19 02:39:43 PM UTC | 0 ⭐ | 7255 👁️ | Never ⏰ | []
SQL |

930 B

|

None

|

0 👍

/

0 👎

Referencia: 
https://firebirdsql.org/file/documentation/release_notes/html/en/3_0/rnfb30-psql-ddltriggers.html 
 
CREATE OR ALTER TRIGGER TRG_PADRAO_CRIA_TAB
active after CREATE TABLE POSITION 0
AS
DECLARE variable vs_Tabela VARCHAR(50);
BEGIN
   
   vs_Tabela = rdb$get_context('DDL_TRIGGER', 'OBJECT_NAME');
 
   -- Cria a PK
   EXECUTE statement 'alter table ' || :vs_Tabela  || ' add constraint PK_' || :vs_Tabela || ' primary key (ID)';
 
   -- Cria o generator
   EXECUTE statement 'create sequence GEN_' || :vs_Tabela  || ' start with 0 increment by 1';
 
   -- Cria a trigger que alimenta o generator
   EXECUTE statement
'create or alter trigger TRG_' || :vs_Tabela || '_BI0 for ' || :vs_Tabela ||
' active before insert position 0
as
begin
 
   /*
      Sobre...: Novo registro
      Revisao.: 1
   */
 
   if ((new.ID is null) or (new.ID<=0)) then
      new.ID = gen_id(GEN_'|| :vs_Tabela || ',1);
 
end';
 
END

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎