• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

need quick help with sql

i have an insert statement that creates a new record. The primary key is an autoincrement, and I need to return that number during or right after the insert statement.

any advice?
 
there's a sql function for this: LAST_INSERT_ID()

if you're using php, you can use mysql_insert_id() in your php code
 
Originally posted by: TechBoyJK
it needs to work with ms sql

SQL Server has a couple of different ways of grabbing last inserted IDs

SELECT @@IDENTITY;

SELECT SCOPE_IDENTITY();

There's a 3rd one that I don't remember off the top of my head, but I use SCOPE_IDENTITY() usually.
 
well I'm using coldfusion, so i wrapped two queries in a <cftransaction> tag.

I do the insert query, and within the same transaction tag, i run another query to find the max id of the table I just inserted to.

I'm still curious as to how to implement the select scope identity. do i use that during the same query that creates the record?
 
Originally posted by: TechBoyJK
well I'm using coldfusion, so i wrapped two queries in a <cftransaction> tag.

I do the insert query, and within the same transaction tag, i run another query to find the max id of the table I just inserted to.

I'm still curious as to how to implement the select scope identity. do i use that during the same query that creates the record?

Yup, separate the Insert query and the Select query by a semicolon

INSERT INTO ...;
SELECT Scope_Identity();

It will return the Identity value back to your recordset after you fire off the query.
 
Originally posted by: TechBoyJK
well I'm using coldfusion, so i wrapped two queries in a <cftransaction> tag.

I do the insert query, and within the same transaction tag, i run another query to find the max id of the table I just inserted to.

I'm still curious as to how to implement the select scope identity. do i use that during the same query that creates the record?

Stick the insert and retrieval of the value in a stored procedure and just run the stored procedure.
 
Back
Top