Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 @@Identity
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

kolucoms6
Average Member

845 Posts

Posted - 14 August 2007 :  01:50:49  Show Profile
After I run below Code, it returns me ZERO.Any Help ?

'+++++++++++++++++++++++++++++++++++++++++
'Get last Generated Unique ID , required to Update
'+++++++++++++++++++++++++++++++++++++++++
Dim lsSQL, loRs
lsSQL = "SELECT @@IDENTITY AS NewID"
Set loRs = adCn.Execute(lsSQL)
llID = loRs.Fields("NewID").value
Response.write "loRs.Fields(NewID).value" & loRs.Fields("NewID").value
'+++++++++++++++++++++++

Edited by - kolucoms6 on 14 August 2007 01:51:22

Doug G
Support Moderator

USA
6493 Posts

Posted - 14 August 2007 :  03:07:22  Show Profile
You have to use @@identity right after you do an insert into a table, and the table must have be using an identity/autonumber primary key on the table.

======
Doug G
======
Computer history and help at www.dougscode.com
Go to Top of Page

kolucoms6
Average Member

845 Posts

Posted - 14 August 2007 :  05:33:08  Show Profile

Just afer Update statement,I have following code.My table has one Primary Key column also.

RS.Update

'+++++++++++++++++++++++++++++++++++++++++
'Get last Generated Unique ID , required to Update
'+++++++++++++++++++++++++++++++++++++++++
Dim lsSQL, loRs
lsSQL = "SELECT @@IDENTITY AS NewID"
Set loRs = adCn.Execute(lsSQL)
llID = loRs.Fields("NewID").value
' Response.write "loRs.Fields(NewID).value" & loRs.Fields("NewID").value
'+++++++++++++++++++++++
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 14 August 2007 :  05:38:16  Show Profile  Visit HuwR's Homepage
it has to be after an insert, @@Identity returns the identity of a newly INSERTED record
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 14 August 2007 :  06:07:09  Show Profile
If it's an UPDATE statement then surely you already have some way of identifying the record?


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page

kolucoms6
Average Member

845 Posts

Posted - 14 August 2007 :  06:26:55  Show Profile

Its like



RS.ADDNew
[Statement]
[Statement]
[Statement]
RS.Update

+++++++++++++++++++++++++++++++++++++++++
'Get last Generated Unique ID , required to Update
'+++++++++++++++++++++++++++++++++++++++++
Dim lsSQL, loRs
lsSQL = "SELECT @@IDENTITY AS NewID"
Set loRs = adCn.Execute(lsSQL)
llID = loRs.Fields("NewID").value
' Response.write "loRs.Fields(NewID).value" & loRs.Fields("NewID").value
'+++++++++++++++++++++++
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 14 August 2007 :  06:59:57  Show Profile  Visit HuwR's Homepage
you are using a recordset which is what is causing your problem. @@Identity is used as follows

lsSQL = "INSERT INTO blah...blah;"
lsSQL = lsSQL & "SELECT @@IDENTITY AS NewID"
Set loRs = adCn.Execute(lsSQL)
llID = loRs.Fields("NewID").value
' Response.write "loRs.Fields(NewID).value" & loRs.Fields("NewID").value
Go to Top of Page

kolucoms6
Average Member

845 Posts

Posted - 14 August 2007 :  07:10:35  Show Profile

So, does it means I CANT use Identity with Addnew feature ?
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 14 August 2007 :  07:38:42  Show Profile  Visit HuwR's Homepage
to be honest you shouldn't really be using the recorset addnew functionality at all, it is pretty bad, recordsets should be used for retrieving data not inserting it, they can cause all kinds of locks and contentions which are bad for the server, it is much better to use an INSERT SQL statement.
Go to Top of Page

kolucoms6
Average Member

845 Posts

Posted - 14 August 2007 :  08:13:47  Show Profile

ahh Ok...

I have around 35 fields need to added :-(

If you suggest so, I will change it..
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 14 August 2007 :  08:44:19  Show Profile  Visit HuwR's Homepage
shouldn't make any difference, it is no more work to do something like this instead

lsSQL = "INSERT INTO TABLE (field1,field2,field3.....) VALUES("
lsSQL = lsSQL & "value1,"
lsSQL = lsSQL & "value2," etc etc
Go to Top of Page

kolucoms6
Average Member

845 Posts

Posted - 14 August 2007 :  08:59:35  Show Profile
Ok
Go to Top of Page

Doug G
Support Moderator

USA
6493 Posts

Posted - 14 August 2007 :  15:21:57  Show Profile
In most cases the new ID value is immediately available in a recordset if you use AddNew and Update, as long as the ID column is included in the recordset. However some recordset cursor types/locations may prevent the ID from showing in your recordset. Using actual SQL as HuwR showed is a better solution in most cases.

======
Doug G
======
Computer history and help at www.dougscode.com
Go to Top of Page

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 14 August 2007 :  19:15:06  Show Profile  Send pdrg a Yahoo! Message
As @@Identity is session-based (so a multiuser system gives accurate results), I believe you need to request it as a part of the same connection session (in the case of a connection-pooled web-db app, that means getting the value as a part of the same db hit)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.25 seconds. Powered By: Snitz Forums 2000 Version 3.4.07