Subject Re: Inserting new record and using a unique generated number - help
Author bossman@i4free.co.nz
Hi Helen,

I'm using IBConsole to do the creating of table, triggers etc etc.

So when i see the text used to create the trigger disappear after
clicking on the run button i'd expect it to be IN the database.

In fact when i look at the table and go to the last TAB on the
resulting window i see the trigger name (as i'd typed it appear) as
an object.

All this is an experiment/learning database.

my comments spread within the body of yours where relivant spaced a
bit in from the left margin.

Dave Lilley
A frustrated firebird.

--- In ib-support@y..., Helen Borrie <helebor@d...> wrote:
> At 12:28 AM 29-06-01 +0000, you wrote:
> >problem:
> >
> >got a table for ease of typing say 3 feilds like those below
> >
> > SET SQL DIALECT 1;
> >
> > DEFAULT CHARACTER SET ASCII */
> >/* Domain definitions */
> >CREATE DOMAIN CUSTNUM AS INTEGER;
> >CREATE DOMAIN ORDERNUM AS INTEGER;
> >
> >/* Table: CUSTOMERS, Owner: SYSDBA */
> >
> >CREATE TABLE CUSTOMERS
> >(
> > LASTNAME CHAR(30) CHARACTER SET ASCII,
> > FIRSTNAME CHAR(30) CHARACTER SET ASCII,
> > HOMEPHONE CHAR(15) CHARACTER SET ASCII,
> > CUSTNUM NUMERIC(9, 0)
> >);
> >
> >CREATE GENERATOR CUSTNUM_GEN;
Yup this Does exist and when looked at shows the next valid
number
> >
> >/* Triggers only will work for SQL triggers */
> >
> >CREATE TRIGGER SET_CUSTNUM FOR CUSTOMERS
> >ACTIVE BEFORE INSERT POSITION 0
> >AS
> > BEGIN
> > NEW.CUSTNUM = GEN_ID(CUSTNUM_GEN, 1);
> > END
> > ^
> >
> >COMMIT WORK ^
> >SET TERM ;^
To get the above i just copied the sample from the SQL HELP that
comes with IBConsole, changes the column name and generator name.

> >now what i want to do is...
> >
> >every time a new record is inserted to have the trigger shown fire
> >and place a new unique number in the field. so far i've not had
any
> >success.
> >
> >I've looked at the help files i've got (in pdf format) and took
the
> >example they showed for creating a trigger and renamed the field
they
> >used to my fieldname. This i thought would be all i'd need to do
but
> >it's proven wrong.
>
>
> You don't say in what way you have "had no success" but...
When i got beyond the last row in the data grid with the
trigger i'd expect to see a number not a NULL value, I get a
NULL value.

> Did you check to see whether the trigger actually exists?
How ? I looked at the objects tab and saw it there, but no where
else (except the metadata).

> If you ran the code above as a script, then the generator would not
exist at the time the engine was trying to compile the trigger. You
should have got an error in the script; and you would find that the
trigger was not created.
Err as mentioned above i'm using IBConsole is there something
better I should use (making it easier on me) ?
>
> It's probable that the generator does exist. Try creating the
trigger interactively, remember to commit it; and then test your
insertions again.
>
> Also, I don't understand why you created the CUSTNUM domain as a 32-
bit integer and then didn't use it, instead creating the CUSTNUM
column as numeric(9,0)....
Well this is a bit hairy for me too...
I think i'm on the Right track here but correct me if i'm wrong.

If i used the domain CUSTNUM and had no table column define as
custnum it'd be a "sin". SO if i create them both (as shown in the
original message) how do i get the domain name CUSTNUM to control
the table column ?.

Also can the Domain name be referance instead of the table column
in other SQL statements etc (Lo think i'm missing the point of
Domains here).

Hope i've not opened up a can of worms here Dave Lilley.

> Helen
>
>
> All for Open and Open for All
> InterBase Developer Initiative ยท http://www.interbase2000.org
> _______________________________________________________