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.
Sheldon LivingstonConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.