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 |
|
Cine
Starting Member
2 Posts |
Posted - 2006-07-28 : 06:47:24
|
| Hi,I have a problem that I want to allocate a range of IDs for use in import.What we currently do is:1 begin transaction2 DECLARE @seed BIGINT3 SET @seed = IDENT_CURRENT('resource') + 1 + 14 DBCC CHECKIDENT('resource', RESEED, @seed)5 COMMITproblem is that sometimes between 3 and 4 another thread inserts a record and then the import will fail, because it will insert a record that already exists.The problem is that IDENT_CURRENT only gets a sch-s lock on the table and DBCC CHECKIDENT obtains a SCH-M lock. Is there any possible way to force a SCH-M lock?Or a completly other way of doing this? |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2006-07-28 : 09:55:12
|
| Change ALL processes that insert records...to check an external flag to see if an 'import' is in progress - flag set "no other insert allowed". And in the import process...set and un-set the flag at the start and end.Alternatively, put the DB into single-user mode...drastic....but a solution. |
 |
|
|
Cine
Starting Member
2 Posts |
Posted - 2006-07-31 : 04:36:01
|
| Actually I found a solution which seems to work.1 begin transaction2 DECLARE @seed BIGINT3 SELECT top 1 @seed = ID FROM resource WITH (TABLOCKX)4 SET @seed = IDENT_CURRENT('resource') + 1 + 15 SET IDENTITY INSERT resource ON6 INSERT INTO resource (id, xxx) values (@seed, yyy)7 SELECT @@IDENTITY8 DELETE FROM resource WHERE ID = @@IDENTITY9 COMMIT |
 |
|
|
|
|
|