Sau đây là câu lệnh dùng kỹ thuật CTE trong SQL Server, vì không có ràng buộc phải order theo 1 tiêu chí nào, nên ta dùng Select Null:
WITH C AS ( SELECT orderid, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum FROM Sales.MyOrders ) UPDATE C SET orderid = rownum;
Bây giờ select lại table MyOrders, ta thấy cột orderid có giá trị tăng dần như hình sau:

0 comments:
Post a Comment