Order guarantee for identity assignment in multi-row insert in SQL Server

后端 未结 2 1270
萌比男神i
萌比男神i 2020-12-21 17:09

When using a Table Value Constructor (http://msdn.microsoft.com/en-us/library/dd776382(v=sql.100).aspx) to insert multiple rows, is the order of any identity column populate

2条回答
  •  梦毁少年i
    2020-12-21 17:13

    Piggybacking on my comment above, and knowing that the behavior of an insert / select+order by will guarantee generation of identity order (#4: from this blog)

    You can use the table value constructor in the following fashion to accomplish your goal (not sure if this satisfies your other constraints) assuming you wanted your identity generation to be based on category id.

    insert into thetable(CategoryId, CategoryName)
    select *
    from
      (values
        (101, 'Bikes'),
        (103, 'Clothes'),
        (102, 'Accessories')
      ) AS Category(CategoryID, CategoryName)
    order by CategoryId
    

提交回复
热议问题