|
-
Aug 8th, 2002, 06:58 AM
#1
Thread Starter
Fanatic Member
ADODB / @@Identity problem.
The following code works fine on my PC (SQL 7.0) but errors when running on another Machine (with SQL 2000) installed.
The tables should be the same on both machines so what else may be causing this error?
VB Code:
Set Conn = Server.CreateObject("ADODB.Connection")
Query = "DECLARE @Email varchar(255) SELECT @Email = (SELECT uemail FROM aamem WHERE ukey = " & IsContactedBy & ") INSERT INTO AAMem (UTitle, UFName, ULastC, UEmail, UCtry, URandomID, UPassword, Visits, UFNameEng, ULastCEng, IsContactedBy) VALUES ('" & UTitle & "','" & UFName & "','" & ULastC & "',@Email,'" & UCtry & "','" & URandomID & "','" & UPassword & "','" & Visits & "','" & UFNameEng & "','" & ULastCEng & "','" & IsContactedBy & "');select @@identity"
Conn.Open(Application("ConnectionString"))
Set RS = Server.CreateObject("ADODB.RecordSet")
Set RS = Conn.Execute (Query).NextRecordset
JustInsertedID = RS(0)
NB the error is "Item cannot be found in the collection corresponding to
the requested name or ordinal"
on the
JustInsertedID = RS(0) line
-
Aug 8th, 2002, 07:01 AM
#2
Fanatic Member
Hi Martin,
Does it help if you do "select @@identity as My_Identity" ?
-
Aug 8th, 2002, 07:03 AM
#3
Thread Starter
Fanatic Member
Thanks, I'll give that a shot.
-
Aug 8th, 2002, 02:17 PM
#4
Martin.
It seems you have to run the query WITHOUT ASSIGNING IT TO A RECORDSET OBJECT and then run the SELECT @@IDENTITY query after it (not in the same step) similar to:
objConn.Execute "UPDATE something SET somethingelse . ...."
Set objRS = objConn.Execute("SELECT @@IDENTITY")
if not objRS.eof then response.write (or debug.print) objRS.Fields(0)
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Aug 8th, 2002, 02:50 PM
#5
Fanatic Member
You could also try setting the "Set NoCount" property of the query:
VB Code:
Set Conn = Server.CreateObject("ADODB.Connection")
Query = "[b]Set NoCount On[/b] DECLARE @Email varchar(255) SELECT @Email = (SELECT uemail FROM aamem WHERE ukey = " & IsContactedBy & ") INSERT INTO AAMem (UTitle, UFName, ULastC, UEmail, UCtry, URandomID, UPassword, Visits, UFNameEng, ULastCEng, IsContactedBy) VALUES ('" & UTitle & "','" & UFName & "','" & ULastC & "',@Email,'" & UCtry & "','" & URandomID & "','" & UPassword & "','" & Visits & "','" & UFNameEng & "','" & ULastCEng & "','" & IsContactedBy & "') select @@identity"
Conn.Open(Application("ConnectionString"))
Set RS = Server.CreateObject("ADODB.RecordSet")
Set RS = Conn.Execute (Query)
JustInsertedID = RS(0)
I also removed the semicolon before the select @@identity and the .NextRecordset.
You can try and see if this works.
Chris
Master Of My Domain
Got A Question? Look Here First
-
Aug 9th, 2002, 03:45 AM
#6
Thread Starter
Fanatic Member
Thanks all of you, I ended up going with
VB Code:
Query = "SET NOCOUNT ON;DECLARE @Email varchar(255) SELECT @Email = (SELECT uemail FROM aamem WHERE ukey = " & IsContactedBy & ") INSERT INTO AAMem (UTitle, UFName, ULastC, UEmail, UCtry, URandomID, UPassword, Visits, UFNameEng, ULastCEng, IsContactedBy) VALUES ('" & UTitle & "','" & UFName & "','" & ULastC & "',@Email,'" & UCtry & "','" & URandomID & "','" & UPassword & "','" & Visits & "','" & UFNameEng & "','" & ULastCEng & "','" & IsContactedBy & "');SELECT @@IDENTITY AS NewID;"
Conn.Open(Application("ConnectionString"))
Set RS = Server.CreateObject("ADODB.RecordSet")
Set RS = Conn.Execute (Query)
JustInsertedID = RS("NewID")
and that seems to have done the trick. The bizarre thing is however that I am using my original technique in 2 other pages throughout the project (following bog standard inserts) and they seem to be performing as expected. I don't have access to the other server to do any testing on it (I am sending my customer the pages and they are putting them up on there and feeding back any problems). But is it possible that in SQL 2000 For some reason the DECLARE @Email varchar(255) may implicitly set no count on or in some other way affect the RecordSet(s) returned?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|