I have the following sql code. Is it guaranteed that MyTable is going to be sorted by MyTable.data? Basically the question is – if I am inserting multiple rows with one INSERT statement, can other connection get in the middle of my insertion and insert something else in between my rows?
CREATE TABLE MyTable(
id bigint IDENTITY(1,1) NOT NULL,
data uniqueidentifier NOT NULL,
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED (id ASC)
)
DECLARE @data uniqueidentifier
SET @data = NEWID()
WITH Dummy AS
(
SELECT @data as data, 1 as n
UNION ALL
SELECT @data, n + 1
FROM Dummy
WHERE n < 100
)
INSERT INTO MyTable(data)
SELECT data FROM Dummy
Thanks.
By definition, SQL tables have no defined order. But I don’t think this is what you are asking. I think you are asking whether another process could insert a row in between one of your inserts. The answer is yes, unless you have the entire table locked, which you probably do not want to do for concurrency reasons.