Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,837 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,567 people online right now. Registration is fast and FREE... Join Now!




How to return value of last autonum entered

 
Reply to this topicStart new topic

How to return value of last autonum entered, Using Scope_Identity

Nolan
20 Sep, 2008 - 12:32 PM
Post #1

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 47


My Contributions
I have an INSERT statement to create a new record and I would like to return the ID value from the newly created record. I believe I have to use the Scope_Identity() function but not sure how to actually write it out to my page after it's returned.
CODE

NEW_PSR_SQL="INSERT INTO tbl_PSRs (PSR_Date, PSR_Text, PSR_Calc_Value, PSR_Override_Value, Is_Override) VALUES ('"&PSR_Date &"','"&PSR_Text &"'," &PSR_Calc_Value &"," &PSR_Override_Value &"," &PSR_Is_Override &") SELECT NEWID = SCOPE_IDENTITY() "

set RsPSR=oConn.execute(NEW_PSR_SQL)
response.write  RsPSR(0) ' trying to return the last autonum from Scope_Identity() - not working


The insert is working fine, but the code to write out the new ID is not working.
Any ideas?

User is offlineProfile CardPM
+Quote Post

Jayman
RE: How To Return Value Of Last Autonum Entered
20 Sep, 2008 - 04:52 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,957



Thanked: 43 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Which database are you working with?
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: How To Return Value Of Last Autonum Entered
21 Sep, 2008 - 05:56 AM
Post #3

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 375



Thanked: 19 times
Dream Kudos: 25
My Contributions
I use this with SQL Server 2005....

sql

SELECT cast(SCOPE_IDENTITY() as bigint) AS 'ColumnName'

User is offlineProfile CardPM
+Quote Post

Nolan
RE: How To Return Value Of Last Autonum Entered
21 Sep, 2008 - 07:52 AM
Post #4

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 47


My Contributions
Sorry - Using SQL Express 2005 backend and native .asp to execute the insert.

What I'm looking for is the specific syntax that will allow me to dislpay the last ID inserted.
I will try Eclipsed's code and let y'all know.



User is offlineProfile CardPM
+Quote Post

Nolan
RE: How To Return Value Of Last Autonum Entered
21 Sep, 2008 - 09:15 AM
Post #5

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 47


My Contributions
Ok so I'm sure I'm getting close but this is just not working:
CODE

PSR_ID_SQL="SELECT cast(SCOPE_IDENTITY() as bigint) AS tbl_PSRs"
set RsPSR_ID = oConn.execute(PSR_ID_SQL)
Response.write "The ID: " &RsPSR_ID(0)
response.end



The recordset appears to be empty, as nothing is displayed after the "The ID:" bit.

I'm getting around this right now by requerying the table for the highest value in the field immediately after insert, but I know this could lead to severe problems if someone creates a record between the insert and the query.
I really want to use Scope_Identity().

What am I missing?????????????????????????????????????????????????????
User is offlineProfile CardPM
+Quote Post

Jayman
RE: How To Return Value Of Last Autonum Entered
21 Sep, 2008 - 09:50 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,957



Thanked: 43 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Try replacing SCOPE_IDENTITY() with @@IDENTITY.

CODE

NEW_PSR_SQL="INSERT INTO tbl_PSRs (PSR_Date, PSR_Text, PSR_Calc_Value, PSR_Override_Value, Is_Override) VALUES ('"&PSR_Date &"','"&PSR_Text &"'," &PSR_Calc_Value &"," &PSR_Override_Value &"," &PSR_Is_Override &") SELECT NEWID = @@IDENTITY "

User is offlineProfile CardPM
+Quote Post

Nolan
RE: How To Return Value Of Last Autonum Entered
21 Sep, 2008 - 09:52 AM
Post #7

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 47


My Contributions
I'll try that Jay, but I'm still unclear as to how I access that value with ASP...

Will it be returned as rs(0) ????

User is offlineProfile CardPM
+Quote Post

weedweaver
RE: How To Return Value Of Last Autonum Entered
22 Sep, 2008 - 02:29 AM
Post #8

New D.I.C Head
*

Joined: 1 May, 2007
Posts: 22


My Contributions
QUOTE(Nolan @ 21 Sep, 2008 - 10:52 AM) *

I'll try that Jay, but I'm still unclear as to how I access that value with ASP...

Will it be returned as rs(0) ????


Try using execute scalar instead:

newID = objCommand.ExecuteScalar

Regards



User is offlineProfile CardPM
+Quote Post

baavgai
RE: How To Return Value Of Last Autonum Entered
22 Sep, 2008 - 03:28 AM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,040



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Is this really ASP.NET? Looks like old style ASP from here.

You forgot a ";", e.g.
CODE

NEW_PSR_SQL="INSERT INTO tbl_PSRs (PSR_Date, PSR_Text, PSR_Calc_Value, PSR_Override_Value, Is_Override) VALUES ('"&PSR_Date &"','"&PSR_Text &"'," &PSR_Calc_Value &"," &PSR_Override_Value &"," &PSR_Is_Override &"); SELECT SCOPE_IDENTITY() "


Also, avoid @@IDENTITY if you can help it. Basically, the older @@IDENTITY has a chance to get confused during multiple transactions. More here.

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: How To Return Value Of Last Autonum Entered
22 Sep, 2008 - 04:33 AM
Post #10

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 375



Thanked: 19 times
Dream Kudos: 25
My Contributions
QUOTE(baavgai @ 22 Sep, 2008 - 07:28 AM) *

Is this really ASP.NET? Looks like old style ASP from here.

You forgot a ";", e.g.
CODE

NEW_PSR_SQL="INSERT INTO tbl_PSRs (PSR_Date, PSR_Text, PSR_Calc_Value, PSR_Override_Value, Is_Override) VALUES ('"&PSR_Date &"','"&PSR_Text &"'," &PSR_Calc_Value &"," &PSR_Override_Value &"," &PSR_Is_Override &"); SELECT SCOPE_IDENTITY() "


Also, avoid @@IDENTITY if you can help it. Basically, the older @@IDENTITY has a chance to get confused during multiple transactions. More here.


agreed. I have gotten caught using the @@IDENTITY.

User is offlineProfile CardPM
+Quote Post

Nolan
RE: How To Return Value Of Last Autonum Entered
23 Sep, 2008 - 08:32 PM
Post #11

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 47


My Contributions
Yup. Ya got me. Plain old .asp.

Don't see a link to that forum on the left tho... thought maybe someone would have an idea in this forum.

I have read TONS about @@identity etc... vs. Scope_Identity() and I know enough to use Scope_Identity in this particular situation.

All I'm really looking for is some code.. probably very simple...

I have a data connection.
I have a select statement.
I do not have the last record Just Entered.

If anyone has EVER done this in the past, and would like to share the one line of code I am looking for, I'd really appreciate it.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 05:04PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month