Solved

how to make field auto increment ?

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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