Are composited statements like this auto trasactionalized or must they be placed in a transaction?
DECLARE @ProjID int = 5024, @Text varchar(100) = 'test'
INSERT INTO ProjectNotes (ProjectID, Text, SortOrder) SELECT @ProjID, @Text, (SELECT ISNULL(Max(SortOrder), 0) + 1 FROM ProjectNotes WITH (HOLDLOCK) WHERE ProjectID = @ProjID)
The HOLDLOCK here prevents another concurent insert from inserting the same next SortOrder value guaranteeing the the values in SortOrder are unique per ProjectID. The HOLDLOCK needs a transaction context to work but I am not sure if this entire INSERT statement including the sub SELECTs are transactionalized (within the context) by default or does this have to be wrapped in an explicit transaction.
In other words, without an explicity BEGIN TRANS, COMMIT TRANS how long does the HOLDLOCK hold the locks in this example. Does it hold them only for the MAX SELECT, or also during the outer INSERT?