Matt,You'd need to override NewId() to be empty, then after each insert you need to call SELECT ScopeIdentity() to
? loSql.ExecuteNonQuery("insert into blah (field1,field2) values (1,2)")
? loSql.Executenonquery("select Scope_Identity()")
Alternately you can also use INSERT INTO ... OUTPUT INSERT.* which returns the entire new record in SQL 2008 and later.
+++ Rick ---
So, if I use CreateNewId() to pre-assign a PK it's all pretty easy to then use this PK as a foreign key on a few new child records that I also need to save.
However, what if I want to setup SQL Server to use an Identity column and let it assign the PK when the record is added... What's the best strategy to use to get that PK right back so I can use it for FK in child records???
Hmm... yeah that code is pretty old...
This will do it:
EXEC dbo.sp_executesql @statement = N'ALTERPROCEDURE [dbo].[sp_ww_NewID]
@cName char(30),
@nRetval int OUTPUTASUPDATE wws_Id SET id = id + 1,
@nRetval = id + 1WHERE TableName = @cNameif @@ROWCOUNT< 1BEGINinsertinto wws_id (Tablename,Id) values (@cName,1)set @nRetval = 1END
'ENDGO
The code doesn't need to check for next id because the Update is a single statement that intrinsicly runs in a transaction. it's not possible for for two updates to run completely simultaneously on the same table while the table is looking at a specific value as it locks the record (or possibly even the entire table) during the update.
+++ Rick ---
Rick - I've noticed that the CreateNewId() method, when working with FoxPro DBF tables, will add the tablename to the ww_id table if it is not already present, but in Sql Server mode, it uses a stored proceure which does not add the table name to wws_id if it is not already present (like it does on the FoxPro datamode). It also does not attempt to verify that the next PK value is not already used in the target table, as it does for the FoxPro DBF version.
I'm moving my app from DBFs to Sql Server, and it looks like you've just got to manually prep the wws_id table with each table name and the correct next ID value to use.