| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
|  |  | |||
Home -> Community -> Usenet -> c.d.o.server -> Re: Oracle Primary key and Delphi: How to insert the primary key value automatically ?
> Questions:
>=20
> 1. Is it possible to use Oracle Database triggers to automatically =
 fill in
> the next sequence number in the primary key field ?=20
>     If so how can this be achieved ?
You can do the following:
Type in command in any SQL Processor (SQL*Plus, ...)
Create Trigger <TriggerName>
before Insert On <TableName>=20
For Each Row
when (:NEW.<PrimKeyColumn> is NULL )
begin
   select <SEQ>.nextval=20
   into :NEW.<PrimKeyColumn>
   from DUAL;
end;
/
=20
> 2. If this is not possible I would like to know the following : When I =
 have
> a table open in a grid open in Delphi and I am in the edit           =20
> mode. When I insert a record can I then get the next sequence number =
 from
> Oracle and fill it myself in the table ? How do I do         that.
You can do the following :
In AfterInsert Event Handler of TQuery=20
asc Oracle about next sequence number ( just use another Query ) and =
assign it to=20
<Query>.FieldByName(<PrimKeyColumn>).AsInteger
If you want your record set to have no "holes" in sequence numbers,=20
use first method.=20
But you must requery the source to get generated numbers ( it may be =
important
if you use Master-Detail relationship ). In this case second method =
should be
preferred.
Sorry for possible mistakes.
Best regards.
Dmitry.
moprok_at_ropnet.ru
Received on Mon Sep 22 1997 - 00:00:00 CDT
|  |  |