Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 21st, 2003, 04:22 AM
IvanWSL IvanWSL is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 IvanWSL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ASP+M$SQL: @@identity problem

Dear all,

I m writing a very typical ASP script which insert a record into one table with an identity column and then get the id generated and insert into another table.

Table_1 columns:
id_1
name

Table_2 columns:
id_1
id_2

But in with the ASP script below, I can't get the id_1 back.
Can anyone help me?

-----
objConn.Open strDSN

strSQL = "insert into Table_1 (name) values ('myName')

objRS.Open strSQL, objConn, adOpenStatic,adLockReadOnly

strSQL= "select @@identity"

Set objRS=objConn.Execute(strSQL)

response.write objRS(0)
-----

Nothing is written to the HTML. When I check the DB, the insert statement has been successfully executed.

I m using IIS5+MS SQL Server 8.
Thanks.

Reply With Quote
  #2  
Old July 21st, 2003, 07:37 AM
MattR MattR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: High above the mucky-muck (Columbus, OH)
Posts: 266 MattR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to MattR
Package it up in a stored procedure.

Reply With Quote
  #3  
Old July 21st, 2003, 08:14 PM
WineIsGood's Avatar
WineIsGood WineIsGood is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: New York
Posts: 49 WineIsGood User rank is Private First Class (20 - 50 Reputation Level)WineIsGood User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 m 51 sec
Reputation Power: 6
MattR is right -- package it in a SP and have the SP return the resulting new ID number to you. However, there is a small flaw in the code you posted so I'll try to clear that up here as well. You need to execute the Insert and get the new ID in a recordset. Normally, however, ASP will pool db connections, so you can't be 100% certain you're still using the same db connection you used to execute the Insert statement, which is important because SQL Server will only return the Identity information to that connection in particular. That's partly why it's a good idea to put this in a SP with error trapping, etc. Anyway, this is the modified code... see if it works for you:

objConn.Open strDSN
strSQL = "insert into Table_1 (name) values ('myName')
objConn.Execute strSQL
strSQL= "select @@identity as Num"
objRS.Open strSQL
response.write objRS.Fields("Num") .Value
objConn.Close

HTH,
-Dave

Reply With Quote
  #4  
Old August 1st, 2003, 11:35 AM
Rich1 Rich1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 1 Rich1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
@@IDENTITY in Stored Procedures

If you're trying to extract the Identity value using a stored procedure, I found out through bitter experience that you need to precede the INSERT statement in the st. proc. with SET NOCOUNT ON. After the insert actions you put SET XYZ=@@IDENTITY (and then SET NOCOUNT OFF is nice). Then in your asp program you get that Identity value with intNewValue = objRs ("XYZ"). It's virtually impossible to find any documentation about doing it this way.

---RLF

Reply With Quote
  #5  
Old August 2nd, 2003, 09:08 PM
Salizar's Avatar
Salizar Salizar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Posts: 289 Salizar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Why not simply write a trigger?

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > ASP+M$SQL: @@identity problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway