select IDENTITY(int,1,1) as SortID from tb_order
仅当 SELECT 语句中有 INTO 子句时,才能使用 IDENTITY 函数。
select IDENTITY(int,1,1) as SortID,* into #Temp from tb_order
无法使用 SELECT INTO 语句将标识列添加到表 '#Temp',该表的列 'ID' 已继承了标识属性。
这样写才是正确的,select 后面不能包含*,不能包含ID号,必须写字段名
select IDENTITY(int,1,1) as SortID,OrderNumber into #Temp from tb_order
select *from #Temp
多表获取ID号
select IDENTITY(int,1,1) as SortID,OrderNumber,a.ID as ID into #Temp from tb_order as a
left join tb_OrderList as b on a.ID=b.OrderID
单表获取ID号,直接写ID会报错的
select IDENTITY(int,1,1) as SortID,OrderNumber,a.ID*1 as ID into #Temp from tb_order as a
ROW_NUMBER() 这样写可以代替
select ROW_NUMBER()over(order by id) as SortID from tb_order
同样获取SortID IDENTITY(int,1,1) 查询速度会快很多