Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SCOPE_IDENTITY() not working 1

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
0
0
GB
I am using SCOPE_IDENTITY() to grab the recently inserted id value of one row to populate another, but it is not working, here is my code

Code:
SQLn = "insert into t_table1 (item1, item2, item3) values ('" & rs("item1") & "', '" & rs("item2") & "', '" & rs("item3") & "')"
objConn.execute SQLn
Set rsIdentity = objConn.Execute("SELECT SCOPE_IDENTITY() FROM t_table1")

response.write rsIdentity(0)

rsIdentity(0) is coming out empty. The t_table1 has an auto identity yes and auto increment. Am I doing something wrong?

Thanks
 
You do not select scope_identity() from a table. From the From t_table1 part and you should be fine.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
From the From t_table1 part and you should be fine.

Uh... yeah. I meant to say,

Remove the From t_table1 part and you should be fine.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
It still isn`t picking the value, I now have

Code:
SQLn = "insert into t_table1 (item1, item2, item3) values ('" & rs("item1") & "', '" & rs("item2") & "', '" & rs("item3") & "')"
objConn.execute SQLn
Set rsIdentity = objConn.Execute("SELECT SCOPE_IDENTITY()")

response.write rsIdentity(0)
 
try...

Code:
SQLn = "Set NoCount On insert into t_table1 (item1, item2, item3) values ('" & rs("item1") & "', '" & rs("item2") & "', '" & rs("item3") & "') SELECT SCOPE_IDENTITY()"

Set rsIdentity = objConn.Execute(SQLn)

response.write rsIdentity(0)

Also, while you're at it, do a little research on [google]sql injection[/google].

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
That worked great thanks, what does the Set NoCount on bit do?

The page is fully sql injectioned up, I quickly rewrote the SQL as the actual SQL on the page is much more convoluted than that :)
 
When you have a query that includes multiple statements, SET NOCOUNT ON prevents a 'phantom' recordset from being returned.

When you run a query in a query window, you can look at the messages tab to see how many rows are affected. If you have a single query/statement, there will be only one value. When you have multiple statements, there are multiple values for 'records affected', so this returns as a recordset AND the data returns as a recordset. The SET NOCOUNT ON statement disables the recordset with the rows affected from being returned.

Just for fun, remove that part. You'll see that your query fails when trying to retrieve the identity value. You'll get a message about the recordset being closed (or something similar). SET NOCOUNT ON prevents that error.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top