ASP Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreASP Programming

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:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old July 21st, 2003, 03:48 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, 01:41 PM
karsh44's Avatar
karsh44 karsh44 is offline
Just another guy
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2003
Location: Wisconsin
Posts: 2,915 karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 13 h 6 m 22 sec
Reputation Power: 76
Code:
<%
dim objConn
dim strSql
dim newID

strSql = "INSERT INTO Table_1(name) Values('myname)"

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "your dsn"
objConn.Open
objConn.execute(strSql)

newID ="SELECT TOP 1 RecordID FROM Table_1 ORDER BY RecordID DESC"

Response.Write newID

objConn.close
Set objConn = Nothing 
%>

I haven't tested this, but I think it should do what you want. I think your problem might have been in the
Code:
Set objRS=objConn.Execute(strSQL) 
response.write objRS(0) 

But I'm not completely sure. Try that, and see if it works. If it does, you're set (you should comment it though, I was lazy). If it throws an error, post it.
HTH
__________________
--Dave--

U2kgSG9jIExlZ2VyZSBTY2lzLCBOaW1pdW0gRXJ1ZGl0aW9uaXMgSGFiZXM=

Reply With Quote
  #3  
Old July 21st, 2003, 09:43 PM
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
I have solved the problem before I see your reply, but thanks anyway, karsh44.

---
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)
---

I think the problem is that I executed the first strSQL at the same time when I open the connection. Then I executed the second one with "objConn.Execute(strSQL)", so the server may treat it as 2 separate transaction and cannot get @@identity as NULL.

My revised code is as follows:
---
objConn.Open strDSN

strSQL = "insert ........ "

objConn.Execute(strSQL)

strSQL= "select @@identity as myID"

Set objRS=objConn.Execute(strSQL)

response.write objRS("myID")
---

Reply With Quote
  #4  
Old July 29th, 2003, 09:12 AM
chriscdn chriscdn is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 1 chriscdn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The answer you accepted, works, kinda

I have done QUITE a bit of sql programming over the last few years, and found the @@identity variable to work only partly. Granted, it gets the last identity value entered...over the whole database. Now, if you doing all of your programming in ASP...this should be acceptable. But, if you start getting into the realm of triggers and such...the following is MUCH better:

sql = "Select Ident_Current('tablename') As Whatever"
set rs = objconn.execute(sql)

newid = rs("whatever") or just rs(0)
rs.close

This makes sure it gets the last identity...from the table you just inserted into.

Chris

Reply With Quote
  #5  
Old August 6th, 2003, 06:11 PM
Doug G Doug G is online now
Grumpier Old Moderator
Dev Shed God 12th Plane (10500 - 10999 posts)
 
Join Date: Jun 2003
Posts: 10,717 Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 39 m 34 sec
Reputation Power: 688
Quote:
Granted, it gets the last identity value entered...over the whole database.

Actually, @@IDENTITY is localized to a database connection, and you have even more granularity with SQL Server 2000 where you can specify which table per connection.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreASP Programming > 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 4 hosted by Hostway