?
Solved

how to make field auto increment ?

Posted on 2004-04-01
3
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

801 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