Solved

ID of last row inserted

Posted on 2003-11-04
3
178 Views
Last Modified: 2009-12-16
How can I get the primary key of the row I just inserted? I am using the mssql_ functions, MS SQL Server

thanks,

Rich
0
Comment
Question by:RichardFox
3 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 50 total points
ID: 9681960
Switch your application over to mySql and use mysql_insert_id().

Oh, you wanted a serious answer.  Try
$query = "SELECT @@IDENTITY";
0
 

Author Comment

by:RichardFox
ID: 9682212
Of course I would use MySQL given a choice!

Thanks jw

Rich
0
 

Expert Comment

by:oscarschulz
ID: 9695759
using @@identity may faile in different cases. I do it like that. I add a additional collum with default NULL to the table.   When inserting a new row i put a value in that column. Then I search for the row with that value and take the ID from that row. For preaparing the next insert you have to set  the value in the additional colllum to Null.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checking CSRF token within a function 36 75
Making API calls from hashed passwords 26 53
Using javascript confirmation for a delete in mysql 5 31
comma true 6 33
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question