Solved

how to make field auto increment ?

Posted on 2004-04-01
3
383 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 20 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now