#1 2014-07-22 10:23:13

tech
Member
Registered: 2014-01-13
Posts: 110

Retrieve new ID value before adding a TSQLrecord

Hi,

I'm about making an invoice system and I've the invoice number composed by the date and an increment, my idea is to retrieve the new ID value and compose the invoice number. Is it possible ?

Thnx,

Offline

#2 2014-07-22 17:59:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Retrieve new ID value before adding a TSQLrecord

Yes of course.

The easiest is to create a calculated field.

Offline

#3 2014-07-24 04:28:49

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Retrieve new ID value before adding a TSQLrecord

I believe before adding a TSQLrecord, you cannot get the ID, and this how sqlite works.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2014-07-24 09:38:23

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Retrieve new ID value before adding a TSQLrecord

Is it not possible to retrieve the MAX id value from table so add 1?

Offline

#5 2014-07-24 09:49:23

martin.suer
Member
Registered: 2013-12-15
Posts: 76

Re: Retrieve new ID value before adding a TSQLrecord

Imagine the record with the highest id has been deleted. Can you tell right away what the next ID will be?
It's an implementation detail and you should never rely on that.

Offline

#6 2014-07-24 10:23:14

tech
Member
Registered: 2014-01-13
Posts: 110

Re: Retrieve new ID value before adding a TSQLrecord

@A.B : on the client side or server side ?

@edwinsn : Yes wat I want is before adding a TSQLRecord.

Offline

#7 2014-07-24 12:46:02

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Retrieve new ID value before adding a TSQLrecord

martin.suer wrote:

Imagine the record with the highest id has been deleted. Can you tell right away what the next ID will be?
It's an implementation detail and you should never rely on that.

You are right.

I think you should add a black record retrieve the id then update it with real data. It's not a good way but I think the only way.

Last edited by array81 (2014-07-24 13:07:44)

Offline

#8 2014-07-24 16:09:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Retrieve new ID value before adding a TSQLrecord

Use a calculated field.

I mean, store the id and the date in 2 fields.
Then compute the invoice number by concatenating the 2.
So you do not need to store the invoice number as such.
Then you can extract the id from the invoice number.

No need to do something complicated.

Offline

#9 2014-07-25 10:35:41

tech
Member
Registered: 2014-01-13
Posts: 110

Re: Retrieve new ID value before adding a TSQLrecord

Ok I understand, thx to all.

Offline

Board footer

Powered by FluxBB