Einzelnen Beitrag anzeigen

Benutzerbild von user0815
user0815

Registriert seit: 5. Okt 2007
331 Beiträge
 
Delphi XE2 Professional
 
#13

AW: Firebird Autoincrement

  Alt 1. Dez 2016, 13:37
per Quellcode anlegen

Delphi-Quellcode:
// table
      q.SQL.Add('CREATE TABLE MyTableName (');
      q.SQL.Add(' ID INTEGER NOT NULL,');
      q.SQL.Add(' MyText VARCHAR(75) COLLATE UNICODE,');
      q.SQL.Add(' MyValue FLOAT DEFAULT 0,');
      q.SQL.Add('CONSTRAINT PK_MyTableName PRIMARY KEY (ID))');
      q.ExecSQL;
// Generator
      q.SQL.Clear;
      q.SQL.Add('CREATE GENERATOR MyTableName_GEN');
      q.ExecSQL;

      q.SQL.Clear;
      q.SQL.Add('SET GENERATOR MyTableName_GEN TO 0');
      q.ExecSQL;
// Trigger
      q.SQL.Clear;
      q.SQL.Add('CREATE TRIGGER ID FOR MyTableName ACTIVE BEFORE INSERT POSITION 0 AS');
      q.SQL.Add('begin');
      q.SQL.Add(' if ( (new.ID is null) or (new.ID = 0) )');
      q.SQL.Add(' then new.ID = gen_id(MyTableName_GEN, 1);');
      q.SQL.Add('end');
      q.ExecSQL;
  Mit Zitat antworten Zitat