Something like this?But you need to have that identity column so the "row number" populates during the insert.CREATE TABLE #TidyLoadedData(SeqNo int identity(1,1) ,Field1 nvarchar(4000) null,SeqNo2 int null)Create Unique Clustered Index IX_Load on #tidyloadeddata (SeqNo)INSERT #tidyloadeddataselect '@@',0 UNION allselect 'lots of words here',0 UNION allselect 'more words',0 UNION allselect '@@',0 UNION allselect 'some words',0 UNION allselect '@@',0 UNION allselect 'lots of free text typed in here',0 UNION allselect 'more words',0 UNION allselect 'this has lots of lines1',0 UNION allselect 'this has lots of lines2',0 UNION allselect 'this has lots of lines3',0Select * FROM #tidyloadeddataDeclare @SeqNo intDeclare @Seqno2 int Set @Seqno2 = 0Update #tidyloadeddataSET @SeqNo2 = SeqNo2 = (Case When Field1 = '@@' then @SeqNo2 + 1 else @SeqNo2 end) ,@SeqNo = SeqNoFROM #tidyloadeddataWITH (INDEX(IX_LOAD))Select * FROM #tidyloadeddata
The results are what you describe as necessary and this will be lightning quick for lots and lots of rows.
Poor planning on your part does not constitute an emergency on my part.