Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ID of last row inserted

Posted on 2003-11-04
3
Medium Priority
?
186 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

926 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