[quoted text, click to view] On 13 Apr, 10:44, "Aidy" <a...@noemail.xxxa.com> wrote:
> I'm selecting from TableA into TableB (nothing fancy, just using "insert
> into") and TableB has an IDENTITY column. It is important that the rows are
> inserted contiguously. If two processes are both calling the stored proc at
> the same time and both inserting, say , 1000 rows will sql lock the table
> while one is processing before allowing the second process to insert into
> the table? Or under these circumstances would it be possible that the two
> processes inserted rows in TableB are "interwoven"?
>
> At the moment I'm putting a serialisable transaction around the INSERT just
> in case, but I'd obviously much prefer to remove the transaction but I am
> unaware how SQL will handle the above scenario.
It isn't sensible to use IDENTITY for this IMO. IDENTITY is fine for
surrogate keys but virtually useless for populating any "meaningful"
data in a column. Do you need to see some predetermined serial order
of rows within each transaction? The only fully documented and
reliable method to achieve that is to insert the rows one at a time. I
wouldn't recommend that though.
What you could do would be to allocate the "next" number to each
transaction instead of each row (you could do that with IDENTITY in
another table for example). Then just populate a column with that
value so that it corresponds to a contiguous block of rows at a time.
That way you wouldn't need to serialize your transactions at all.
Alternatively, consider using the ROW_NUMBER() function.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--