What I think you are trying to do:
I think you want to return your newID
values, sorted in ascending order with an incrementing row number in the id
column.
What I think you are misunderstanding:
The ID of a row does not need to be in any particular order, it just needs to be unique. If you are using the incrementing integer value of your id
column as an identifier elsewhere in your solution, then you do not need to worry about the order. This is important because if you were to insert
a new newID
value that when sorted fell between two existing newID
values, some of the id
values would have to change to retain the ordering. This would break any relationships based on the id
value.
It is important to note here that the int identity(1,1)
value automatically increments (not always by 1) for each row as it is inserted. If you insert
your data 'out of order' then the value will also be 'out of order'. I think you are misunderstanding what this functionality is for. In short, it doesn't do what you want it to.
You also can order a uniqueidentifier
column as is. You will be getting an error because you have called the column newID
, which is a reserved keyword within SQL Server. If you want to keep this name (which I suggest you don't), you will need to reference it within square brackets: order by [newID]
. Bear in mind that the 'correct' ordering of a uniqueidentifier
is not the same as the alphabetical ordering of the value you see on the screen, much like how the numeric ordering of 1, 2, 3, 10, 11, 12
is different to alphabetically ordering the same values as 1, 10, 11, 12, 2, 3
.
How to actually get to your desired output:
If on the off chance you really do just want to get the row number of the newID
value that is in your table, you can do this with the row_number
windowed function:
declare @temp table(id int identity(1,1)
,newID UNIQUEIDENTIFIER
);
insert into @temp(newID) values
('9653de71-34c2-4409-bcee-6809e170e197')
,('3f3e7ab8-a516-4dd2-a04b-31feeac8fdea')
,('1f1d38b8-3c31-4479-ba48-b71ce8525ea3')
,('33f1e2b9-f4c3-4e57-9267-ff729a326318');
-- Showing the int identity, which increments as new rows are added
select *
from @temp
order by [newID];
-- Using the row_number function to generate the id value at runtime
select row_number() over (order by [newID]) as id
,[newID]
from @temp
order by [newID];
Outputs
Using int identity
:
+----+--------------------------------------+
| id | newID |
+----+--------------------------------------+
| 2 | 3F3E7AB8-A516-4DD2-A04B-31FEEAC8FDEA |
| 1 | 9653DE71-34C2-4409-BCEE-6809E170E197 |
| 3 | 1F1D38B8-3C31-4479-BA48-B71CE8525EA3 |
| 4 | 33F1E2B9-F4C3-4E57-9267-FF729A326318 |
+----+--------------------------------------+
and using row_number
:
+----+--------------------------------------+
| id | newID |
+----+--------------------------------------+
| 1 | 3F3E7AB8-A516-4DD2-A04B-31FEEAC8FDEA |
| 2 | 9653DE71-34C2-4409-BCEE-6809E170E197 |
| 3 | 1F1D38B8-3C31-4479-BA48-B71CE8525EA3 |
| 4 | 33F1E2B9-F4C3-4E57-9267-FF729A326318 |
+----+--------------------------------------+