Steve-0


You'll have to bear with me as I am not very well versed in databases in general, but even more so with Foxpro.

I am using the latest Foxpro OleDB driver and need to find a way to retrieve the recno for a newly inserted row into a table.

I have tried the following, assuming that the newly inserted row would be the last one in the table but this does not seem to work as I thought it would.

"select recno() as Oid from [TableName]". This is returning the recno of the first item in the table that is not marked for deletion.

Any help would be greatly appreciated.

Thanks,

Steve




Re: Retrieve Recno() of a newly inserted row into a table

dni


If you are using insert you may find it at recno()+1






Re: Retrieve Recno() of a newly inserted row into a table

Steve-0

Sorry for being so daft... but exactly how might I get that Would you mind providing a sample sql statement Thanks!







Re: Retrieve Recno() of a newly inserted row into a table

dni

STORE recno() to var1

INSERT .....

var1 = var1+1

SELECT <Select_field_list or *> FROM YourTable WHERE recno() = var1 ...






Re: Retrieve Recno() of a newly inserted row into a table

CetinBasoz

What would you do with recno() It is not a saved value. Instead it is a calculated value (calculation is done by physical position of a record which may change).

If table is not used by any other user (opened exclusively) then you could get it by:

select top 1 recno() as Oid from [TableName] order by 1 desc

Or even in case of multiple users a stored procedure might do the insert and return current recno() which is the recno() it inserted.

But it is not valid as an objectId (Oid menat that ). The moment table is packed and there is a deleted record(s) before it then it would change. Sorting on a particular order would also change it. Anything that touches physical order of records would change itSmile

Instead use a column or a combination of columns that uniquely identifies a row within a table (primary key).





Re: Retrieve Recno() of a newly inserted row into a table

Steve-0

Thanks!!! That was exactly what I was looking for.

For my purposes I only need a temporary ID used in a session and there aren't any columns or groups of columns that really work well as a primary key.

Thanks again!!!

-Steve






Re: Retrieve Recno() of a newly inserted row into a table

RavindraPatil

Dear dni,

do u mean insert command will insert a new record immediately after the current record and not at the bottom e.g. if my dbf contains 100 records and i use following code

use tmp.dbf

go 50

insert into .........

what will b the new record number 51 or 101




Re: Retrieve Recno() of a newly inserted row into a table

RavindraPatil

is it not possible to find the new record no. with the following command

calc max(recn()) to oid

or

go bottom

oid = recn()





Re: Retrieve Recno() of a newly inserted row into a table

CetinBasoz

If you have 100 records with a code like:

use tmp.dbf

go 50

insert into .........

New record number is 101 (or greater than 101 if there are multiple users inserting records at the same time).





Re: Retrieve Recno() of a newly inserted row into a table

CetinBasoz

No it's not possible to find the new recno() with:

Code Block

calc max(recn()) to oid

in a multiuser environment.

This one wouldn't even work in a single user environment:

Code Block

go bottom

oid = recn()

because it's dependant on current order of the table.





Re: Retrieve Recno() of a newly inserted row into a table

dni

RavindraPatil,

Yes, that is "insert" no "append blank". You may open a table and run the code:

STORE RECNO() TO var1

INSERT BLANK

var1=var1+1

GOTO var1

BROWSE

You will be positionated on the new inserted record.






Re: Retrieve Recno() of a newly inserted row into a table

CetinBasoz

Dni,

I think you know that it's a command that should be avoided even in single user situations and would not work in multiuser.

Please, there are newbies reading these forums too and they may not be aware of why they should avoid it.