• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Trying to use @@identity

Using classic ASP, just want to do and SQL insert and then display the ID of the record just inserted by using @@identity.
0
Sheldon Livingston
Asked:
Sheldon Livingston
1 Solution
 
crisco96Commented:
You're better off to use scope_identity() instead of @@identity: http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

All you need to do is do your insert then in the same command do:

select scope_identity() as RecordID

This will make it so your recordSet will have a RecordID you can pull from.

Post what code you've got and I can help more.
0
 
Sheldon LivingstonConsultantAuthor Commented:
Here is the code:
 
session.LCID = 1033
set objRecordset = server.CreateObject("ADODB.Recordset")
set objConnection = server.CreateObject("ADODB.Connection")
dim theDateTime
if Request.Form("DateTime") = "Enter Date/Time here" then
	theDateTime = "1-1-1900"
end if
objConnection.ConnectionString = OLEDB_SD

objConnection.Open

'Post the event
sqlStatment = "insert into sdEvents " & _
			"(field1, field2) values " & _
			"('" & Request.Form("field1data") & "', '" & theDateTime & "'); select SCOPE_IDENTITY AS theID;"
			
objRecordSet.ActiveConnection = objConnection
set objRecordSet = objConnection.Execute(sqlStatment)
Response.Write "xx" &  objRecordSet.fields("theID")
Response.end

Open in new window


expecting the final line to spit out the ID.
session.LCID = 1033
set objRecordset = server.CreateObject("ADODB.Recordset")
set objConnection = server.CreateObject("ADODB.Connection")
dim theDateTime
if Request.Form("DateTime") = "Enter Date/Time here" then
	theDateTime = "1-1-1900"
end if
objConnection.ConnectionString = OLEDB_SD

objConnection.Open

'Post the event
sqlStatment = "insert into sdEvents " & _
			"(eventTitle, eventDate, eventTime, eventAddress, eventAddress2, eventCity, eventState, eventZip, eventComments, theGroup) values " & _
			"('" & Request.Form("txtEventName") & "', '" & theDateTime & "', '" & theDateTime & "', '" & Request.Form("txtAddress") & "', '" & _
			Request.Form("txtAddress2") & "', '" & Request.Form("txtCity") & "', '" & Request.Form("txtState") & "', '" & Request.Form("txtZip") & "', '" & _
			Request.Form("txtComments") & "', " & Request.QueryString("GP") & "); select SCOPE_IDENTITY AS theID;"
			
objRecordSet.ActiveConnection = objConnection
set objRecordSet = objConnection.Execute(sqlStatment)
Response.Write "xx" &  objRecordSet.fields("theID")
Response.end

Open in new window

0
 
Cenjoy100Commented:
SCOPE_IDENTITY and @@IDENTITY will return last identity values generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.

even you can try this

if you want to return on row basis
USE AdventureWorks2008R2;
GO
DECLARE @MyTableVar table( NewScrapReasonID smallint,
                           Name varchar(50),
                           ModifiedDate datetime);
INSERT Production.ScrapReason
    OUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate
        INTO @MyTableVar
VALUES (N'Operator error', GETDATE());

--Display the result set of the table variable.
SELECT NewScrapReasonID, Name, ModifiedDate FROM @MyTableVar;
--Display the result set of the table.
SELECT ScrapReasonID, Name, ModifiedDate
FROM Production.ScrapReason;
GO
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Sheldon LivingstonConsultantAuthor Commented:
crisco96:  can you tell me where my code is lacking?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please add the SET NOCOUNT ON in your code

session.LCID = 1033
set objRecordset = server.CreateObject("ADODB.Recordset")
set objConnection = server.CreateObject("ADODB.Connection")
dim theDateTime
if Request.Form("DateTime") = "Enter Date/Time here" then
        theDateTime = "1-1-1900"
end if
objConnection.ConnectionString = OLEDB_SD

objConnection.Open

'Post the event
sqlStatment = "SET NOCOUNT ON ; insert into sdEvents " & _
                        "(eventTitle, eventDate, eventTime, eventAddress, eventAddress2, eventCity, eventState, eventZip, eventComments, theGroup) values " & _
                        "('" & Request.Form("txtEventName") & "', '" & theDateTime & "', '" & theDateTime & "', '" & Request.Form("txtAddress") & "', '" & _
                        Request.Form("txtAddress2") & "', '" & Request.Form("txtCity") & "', '" & Request.Form("txtState") & "', '" & Request.Form("txtZip") & "', '" & _
                        Request.Form("txtComments") & "', " & Request.QueryString("GP") & "); select SCOPE_IDENTITY AS theID;"
                        
objRecordSet.ActiveConnection = objConnection
set objRecordSet = objConnection.Execute(sqlStatment)
Response.Write "xx" &  objRecordSet.fields("theID")
Response.end

Open in new window


otherwise, I recommend you start to put your "sql" code into stored procedure, makes things much easier ...
http://www.sql-server-performance.com/2003/stored-procedures-basics/
0
 
Sheldon LivingstonConsultantAuthor Commented:
Still no joy...
0
 
Sheldon LivingstonConsultantAuthor Commented:
angelIII:  It does work... since I posted this I replace @@indentiy with scope_indentity which gave me an error (column scope_identity doesn't exist).

Switching back to @@identity works.

Thanks
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
it must be:

"...select SCOPE_IDENTITY() AS theID;"

it's a function, so you need to brackets. please use SCOPE_IDENTITY() ...
0
 
Sheldon LivingstonConsultantAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now