Retrieve value of autoincrement field with inserted record 
Author Message
 Retrieve value of autoincrement field with inserted record

Hello there,

I have a question...
This has been put on this NewsGroup earlier today but since I don't see it
appear (4 hours later) and this is my first try of using a newsgroup, I
must assume I did something wrong(?)...

When inserting a record in a table with an autoincrement field (the
primary key), either by a TTable or TQuery, how can you retrieve the value
of this field?
Using: delphi 2.0, with DB's: Paradox, InterBase and Sybase (XI)... all
"behave" the same in this respect.

I want to implement a master-detail relation. When a record is inserted in
the master and details are added, I want to be able to cancel the whole
transaction, details and master. To insert the details I need the number
of the master.

Any help is greatly appreciated,



Tue, 15 Dec 1998 03:00:00 GMT  
 Retrieve value of autoincrement field with inserted record

Quote:

> Hello there,

> I have a question...
> This has been put on this NewsGroup earlier today but since I don't see it
> appear (4 hours later) and this is my first try of using a newsgroup, I
> must assume I did something wrong(?)...

> When inserting a record in a table with an autoincrement field (the
> primary key), either by a TTable or TQuery, how can you retrieve the value
> of this field?
> Using: Delphi 2.0, with DB's: Paradox, InterBase and Sybase (XI)... all
> "behave" the same in this respect.

> I want to implement a master-detail relation. When a record is inserted in
> the master and details are added, I want to be able to cancel the whole
> transaction, details and master. To insert the details I need the number
> of the master.

> Any help is greatly appreciated,


Hi,

AFAIK, there is no easy way to get this. I'm using a stored procedure
(with SQL-Database) which inserts and returns the autoincrement value.

ciao
       sm



Fri, 18 Dec 1998 03:00:00 GMT  
 Retrieve value of autoincrement field with inserted record

Quote:

> Hello there,

> I have a question...
> This has been put on this NewsGroup earlier today but since I don't see it
> appear (4 hours later) and this is my first try of using a newsgroup, I
> must assume I did something wrong(?)...

> When inserting a record in a table with an autoincrement field (the
> primary key), either by a TTable or TQuery, how can you retrieve the value
> of this field?
> Using: Delphi 2.0, with DB's: Paradox, InterBase and Sybase (XI)... all
> "behave" the same in this respect.

> I want to implement a master-detail relation. When a record is inserted in
> the master and details are added, I want to be able to cancel the whole
> transaction, details and master. To insert the details I need the number
> of the master.

> Any help is greatly appreciated,


For Paradox Table it's easy - you do not need to do anything because when you post
your changes DataSet make new record current. (I am not sure what happens if you using
filter or master/detail and change reference to master table in your detail)
If you use queries I dont know easy way to do it -
you have to use stored procedures to get indentity (autoincrement) field

Or as a alternative if your application the only application accessing that data
you can create your own function to get next available counter (I would create stored procedure to supply
next counter so not only one apps can use it)



Fri, 18 Dec 1998 03:00:00 GMT  
 Retrieve value of autoincrement field with inserted record

Quote:


>> Hello there,

>> When inserting a record in a table with an autoincrement field (the
>> primary key), either by a TTable or TQuery, how can you retrieve the value
>> of this field?

I had the same problem.  I ended up having to do it in a way not
elegant at all but sometimes you have to get things done now and think
about more elegant solutions later when there is more time.
My method is:  Choose one field on the table, preferably a text field.
When you insert the new record into the table set this field to a
value unusual that will never occur on that field under normal
circumstances like '#$%^&*()-=' for example.
After you insert the record then you can query the table for the value
of the autoincrement field WHERE the text field has the value above.
Then you update the record to place the real value on the text field

I would like to hear from others on a more direct way of inquiring
this value via SQL but at least this works.  /Luis



Sat, 19 Dec 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Retrieving Serial Key from record inserted into Informix DB from Delphi 1

2. ADO AutoIncrement field value?

3. Autoincrement Fields - setting to new value

4. Create a autoincrement-field with a minimum value

5. How to alter value of autoincrement field

6. Reading the value of Autoincrement field

7. DBGrid1.Fields[0] - Where's The Value During Insert/Before Post

8. Inserting record with BLOB field into Interbase

9. Insert record w/SQL Server identity field

10. Get field from currently inserted record

11. Retrieving a Value

12. How to retrieve info from an insert SQL statement

 

 
Powered by phpBB® Forum Software