SQL Server 2005 ROW_NUMBER() without ORDER BY

后端 未结 2 1761
心在旅途
心在旅途 2020-11-29 09:17

I am trying to insert from one table into another using

DECLARE @IDOffset int;
SELECT @IDOffset = MAX(ISNULL(ID,0)) FROM TargetTable

INSERT INTO TargetTable         


        
2条回答
  •  孤独总比滥情好
    2020-11-29 09:57

    You can ignore the ordering by using order by (select null) like this:

    declare @IDOffset int;
    select  @IDOffset = max(isnull(ID, 0)) from TargetTable
    
    insert  into TargetTable(ID, FIELD)
    select  row_number() over (order by (select null)) + @IDOffset, FeildValue
      from  SourceTable
     where  [somecondition]
    

提交回复
热议问题