|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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= |
|
#3
|
|||
|
|||
|
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") --- |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > ASP+M$SQL: @@identity problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|