Subject Re: [firebird-support] create generator from a select
Author Svein Erling Tysvaer
Hi Alejandro!

First, you tell which fields to insert into, then from where to get the
values, i.e. something like:

INSERT INTO TEST (
CLIENTID,
DATE2,
DATE1,
FieldToContainGeneratedValue
)
SELECT T1.CLIENTID,
T1.DATA2,
T1.DATA1,
GEN_ID(MyGenerator, 1)
FROM
T1
WHERE
CONDITIONS
ORDER BY
T1.CLIENTID,T1.DATA1

It is often a good idea to create a BEFORE INSERT trigger, that assigns
a value to the FieldToContainGeneratedValue if it is omitted or contains
0 (e.g. DB Workbench can do this semiautomatically). If you do that, you
can simplify your SQL a little bit:

INSERT INTO TEST (
CLIENTID,
DATE2,
DATE1
)
SELECT T1.CLIENTID,
T1.DATA2,
T1.DATA1
FROM
T1
WHERE
CONDITIONS
ORDER BY
T1.CLIENTID,T1.DATA1

HTH,
Set

Alejandro Garcia wrote:
> Hi! I'm trying to create a generator:
> I have a table with 80 columns, I need to select 3 of them: CLIENTID, DAT=
> E1 and DATE2 and order them by CLIENTID, DATE1 and then create another co=
> lumn that is a sequencial number (a generator) that reflects that order.=20
> I need this to be a new table because I've done a stored procedure assuming=
> that I have this 4 columns table which one of them is a generator.
> =20=20
> I'm trying to do something like this, TEST is my new table that I've alread=
> y created:
> =20=20=20
> INSERT INTO TEST (
> TEST.CLIENTID,
> TEST.DATE2,
> TEST.DATE1,
> GEN_ID(GENID, 1)
> )
> SELECT T1.CLIENTID,
> T1.DATA2,
> T1.DATA1
> FROM
> T1
> WHERE
> CONDITIONS
> ORDER BY
> T1.CLIENTID,T1.DATA1