[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Interbase: How to get the last generator value of newly inserted record?

I'm using Interbase as database server for my Client/Server application. I use combination of trigger and generator for creating the primary key for each table. My problem is, how could I receive the generator value of just-inserted record? Please do remember that we have many users using the application concurrently, receiving the generator value is easy, but I don't know which one is for my session. Currently, I create a stored procedure for inserting value into the database, then return me with the value of the generator. But this way, my code will use stored procedure and will not be compatible with other database. Is there any better idea? I have to migrate this application to other database in the near future, it would be a huge problem if I do it this way.

If you can't answer this question, perhap you can tell me how to get the database connection session id (the id that uniquely generated for each user connection to the database). If I can get the session ID, it wouldn't be hard problem to keep generator value in database along with session ID.
0
kittichai
Asked:
kittichai
1 Solution
 
kretzschmarCommented:
the bahaviour of all autogenerated values is, that there are unknown at the moment of inserting. for getting the last given value you must refetch the last inserted record to reread the automatically given number.

for myself, i read the autogenerated value into the delphifrontend, and supply this value in the beforepost-event for the field for ehat it is needed. this gives me the control for the value and i avoid by this way a refetch of the actually inserted record.

even if you know your session-id, you can't get the correct last for you generated value in a concurrent multiuser-enviroment without refteching the last inserted record, because the generating is session-independent.

meikl ;-)
0
 
ITugayCommented:
Hi kittichai,
meikl :-)

I know one more way. You can add field 'USER_NAME' to the table and write trigger to put username there on insert event. To extract last record you can use something like this:

select max(id_record) from thetable where USER_NAME like 'SYSDBA'

-------
Igor.
0
 
geobulCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

split points between kretzschmar and ITugay

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now