PDA

View Full Version : Last ID


GzArIa
09-27-2004, 04:34 PM
Hi there,

I need to know how I can insert a row into a table with the last ID of a just inserted row.
Like this

Insert Data into Table (Other_ID) VALUES (@@identity of the other table)

is that possible, can I actually use a SQL statement as a value to be inserted,...
thanks,...

raf
09-27-2004, 05:05 PM
what db-format are you using?

this questin has been asked here a number of times, so a search will probably give you the answer for the most common db-servers...

GzArIa
09-27-2004, 05:14 PM
Sorry for the trouble,

i use SQL Server,...
problem was I had also to remove VALUES

Syntax is:

INSERT INTO TABLE(FIELD) Select MAX(ID) FROM TABLE2
OR
INSERT INTO TABLE(FIELD) Select @@IDENTITY FROM TABLE2
although had some problems as it duplicated the id's don't know why so I used
INSERT INTO TABLE(FIELD) Select Distinct @@IDENTITY FROM TABLE2

hope this might help others as well
thanks
GzArIa