Declare @Order INT=0
INSERT INTO MyTable(col1,col2,ordercol) values('abc','xyz',++@Order)
this way order value is not getting incremented, but i found this way value gets incremented during update
declare @i int = SELECT ISNULL(MAX(interfaceID),0) + 1 FROM prices
update prices
set interfaceID = @i , @i = @i + 1
where interfaceID is null
code taken from https://stackoverflow.com/questions/13629382/update-int-column-in-table-with-unique-incrementing-values
if in case of update a variable value getting incremented but the same approach fail during insert...Why?
i saw people advise go for this approach to increment value during insert. here is code example
insert into Table1 (column1,column2)
select
isnull(T1.m, 0) + row_number() over (order by T2.anotherColumn),
T2.anotherColumn
from Table2 as T2
outer apply (select max(column) as m from Table1) as T1
OR
Insert into table1 (col1, col2, col3, col4)
select col1,
isnull(( select max(isnull(col2,0)) from table1 ),0) + ( ROW_NUMBER() over (order by col2) ),
col3, col4
from table2
please tell me few approach by which i can increment value and insert into column. thanks