Solved

how to make field auto increment ?

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 206
LAN or WAN ? 11 106
Installshield for Embarcadero EX 10.1 Berlin 4 73
Delphi android app hide keyboard 3 45
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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