?
Solved

how to make field auto increment ?

Posted on 2004-04-01
3
Medium Priority
?
398 Views
Last Modified: 2010-04-05
Im using IB expert with firebird , how can I make  An integer  field auto Increment (in details please) ?
0
Comment
Question by:sraab2004
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Trekker72 earned 80 total points
ID: 10738960
Hi, you can make do it in a Before Insert trigger for the Table like so...

CREATE TRIGGER MYTABLE_BI FOR MYTABLE
ACTIVE BEFORE INSERT POSITION 0
AS
declare variable MaxID integer;
begin
  if (new.ID is null) then begin
    select max(ID) from usertype into :MaxID;
    if (MaxID is null) then
      MaxID = 0;
    new.ID = :MaxID + 1;
  end
end

or you can create  a generator and use that...

CREATE TRIGGER MYTABLE_BI FOR MYTABLE
ACTIVE BEFORE INSERT POSITION 0
AS
begin
    new.ID = gen_id(MYTABLE_ID_Gen, 1);
end
0
 

Author Comment

by:sraab2004
ID: 10750854
if Iwant to use generator wher should I write the code  which you gave me
0
 
LVL 2

Expert Comment

by:Trekker72
ID: 10751629

-- first create a generator, then create a "Before Insert" trigger for the table you want the auto-inc field in.
-- The following script will set an existing field called "ID" in table "MYTABLE" to be auto-incrementing by
-- creating the generator and trigger for you.  Get yourself a program like IBExpert personal (free) to help you run scripts
-- and create meta objects, etc

CREATE GENERATOR MYTABLE_GEN_ID;
SET GENERATOR MYTABLE_GEN_ID TO 0;

Commit work;

SET TERM ^ ;

CREATE TRIGGER MYTABLE_BI FOR MYTABLE
ACTIVE BEFORE INSERT POSITION 0
AS
begin
  new.ID = gen_id(MYTABLE_ID_GEN, 1);
end

SET TERM ; ^

Commit Work;
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question