Solved

ID of last row inserted

Posted on 2003-11-04
3
176 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 is …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now