开发者

How to retrieve specific field of the record just inserted to DB

i'm using开发者_JAVA技巧 sql server 2008 i insert the data using .Addnew

i have code like this

strSQL = "SELECT * FROM table"
rs.CursorType = 2
rs.LockType = 3
rs.Open strSQL, adoCon
rs.AddNew
rs.fields("pid") = pid
rs.fields("name") = name
rs.update
rs.requery
response.write rs.fields("GUID")
rs.close

i don't get an error - i get a nothing printed

i do in in classic asp, no stored procedure, nothing special the guid column is not a key! it has a default value of newid() and the value is there in the db i just can't get it

what am i doing wrong?


Add SELECT @@IDENTITY to the SQL statement to retrieve the last inserted identity:

Dim rs , cmd
Set rs = server.createobject("ADODB.Recordset")
Set cmd = server.createobject("ADODB.Command")

cmd.ActiveConnection = GetConnectionString()
cmd.CommandType = adCmdText
cmd.CommandText = "INSERT into Table (pid, name) values (1, 'the name') SELECT @@IDENTITY"


rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenForwardOnly, adLockReadOnly

response.write "Last GUID: " + rs(0)


This article explains the means of getting identity value with example code. If you need more help after this let us know

http://databases.aspfaq.com/general/how-do-i-get-the-identity/autonumber-value-for-the-row-i-inserted.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜