Retrieve ID on INSERT query

stone28

Registered User.
Local time
Today, 21:54
Joined
Aug 27, 2009
Messages
58
HI,

Can I create a query which will return ID for me from newly created record?

Something like:

SELECT tbl_users.user_id FROM tbl_users (INSERT INTO tbl_users tbl_users.user_fname, tbl_users.user_lname) VALUES ('bla', 'bls');
 
HI,

Can I create a query which will return ID for me from newly created record?

Something like:

SELECT tbl_users.user_id FROM tbl_users (INSERT INTO tbl_users tbl_users.user_fname, tbl_users.user_lname) VALUES ('bla', 'bls');

No.

However, if ID is an autonumber, you could use

Select max(ID) FROM tbl_users
 
But if I have to do it in a separate query, then I can just ask for ID where fname = 'name'?
 
As long as "name" is unique. Hopefully that was just an example since "name" is a reserved word in many programming languages.
 
Yeh, yeh... it was just to say that if it has to be separate query then it can be anything. I thought maybe it can be done in one query...
 
If it's an autonumber field, you should be able to do it this way:

SELECT @@Identity FROM tbl_Users
 
No.

However, if ID is an autonumber, you could use

Select max(ID) FROM tbl_users
This should work if no records are ever deleted. If records are deleted, the engine might reuse the deleted autonumbers, as far as I know.
 

Users who are viewing this thread

Back
Top Bottom