Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
kennedyclear
Starting Member
2 Posts |
Posted - 2008-04-24 : 08:25:13
|
Can anyone help with this problem.I am doing some mods to an existing db web app in asp. I am providing a facility to copy a subset of related records in a few tables. I am using one recordset to read in the selected records and then copying the data into another recordset creating a new record in the same table. I have to then pick up the @@IDENTITY of the new record so that I can update linked records in other tables.The @@IDENTITY value returned is null unless I close the selection recordset. I have created a simplified version of the code to demonstrate this.The underlying table is called 'test' and it has 2 fields, 'test_id', integer, identity and 'test_desc' varchar(50)ASP Code<%dim objConn, objRS1, objRS2, objRS3sub OpenDatabase() 'open database object here instead of in page Set objConn = Server.CreateObject("ADODB.Connection") 'Connect object to database through global.asa application variable objConn.Open = Application("test_ConnectionString") 'Create recordset object to use Set objRS1 = Server.CreateObject("ADODB.Recordset") Set objRS2 = Server.CreateObject("ADODB.Recordset")end subsub CloseDatabase Set objRS1 = Nothing Set objRS2 = Nothing objConn.Close Set objConn = Nothingend subfunction UnNull(numval) if isnull(numval) then UnNull="NULL" else UnNull=cstr(numval) end ifend functionOpenDatabasestrSQLQuery = "select * from test where test_id in (1,2) order by test_id"objRS1.Open strSQLQuery, objConn, 1, 3do while not objRS1.eof objRS2.Open "test", objConn, 1, 3 objRS2.AddNew old_test_id = objRS1("test_id") objRS2("test_desc") = objRS1("test_desc") objRS2.Update objRS2.Close ' *1 objRS1.Close strSQLQuery = "select @@IDENTITY as new_id" objRS2.Open strSQLQuery, objConn new_test_id=objRS2("new_id") objRS2.close response.write("Old ID = " & UnNull(old_test_id) & ", New ID = " & UnNull(new_test_id)) response.write("<p>") ' *2 response.end objRS1.movenextloopobjRS1.closeCloseDatabase%>OutputOld ID = 1, New ID = NULLOld ID = 2, New ID = NULLIf I unrem the lines *1 and *2 then the output isOld ID = 1, New ID = 11I realise that there are other ways of doing this but this is only a simplified example whereas the actual application is far more complicated.Thanks is advanceKennedy |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-24 : 08:30:45
|
Have a stored procedure which inserts the record for you. Then you can fetch the @@IDENTITY value.But you should probably use SCOPE_IDENTITY() instead.With your code, when you doobjRS2.Update you should get the id updated into the object directly.Try[code]objRS2.Updatedebug.print obrRS2.Fields("test_id").ValueAlso read and understand the concept of SCOPE. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-24 : 08:51:37
|
| SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions because they return values that are inserted into identity columns.IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT (Transact-SQL).SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.Reference from Books online |
 |
|
|
kennedyclear
Starting Member
2 Posts |
Posted - 2008-04-24 : 08:54:59
|
| Thanks Peso, that seems to work ok.I seem to remember testing this system before and it not working reliably, but as it is working now I will make the mods and do some testing.I understand the differences between SCOPE_IDENTITY and @@IDENTITY but since I never use triggers I was happy to continue as was.Thanks for your helpKennedy |
 |
|
|
|
|
|
|
|