You can use the IDENTITY
property for you PK field and can keep the order of the first table, and the give a random order to the second.
You could also specify an order for the table with the NULL
id's, but you'd have to choose what that order is with a conditional order by since the table has no logical order based on what you have provided.
create table #t1 (id int null, code bigint)
create table #t2 (id int null, code bigint)
insert into #t1
values
(NULL,106164029),
(NULL,106100797),
(NULL,106100793),
(NULL,106301098),
(NULL,106010735),
(NULL,206010989),
(NULL,206010956)
insert into #t2
values
(1,102323223),
(2,105454545),
(3,106232244),
(4,106432432),
(5,106043222),
(6,206122222),
(7,211111116)
--here is your final table with the auto calculated id primary key
create table #t3 (id int identity (1,1), code bigint)
alter table #t3 add constraint pk primary key (id)
--the order by keeps the same order as the original table with the id values
insert into #t3
select code from #t2
order by id
--since the ID is null in this table, i didn't specify an order by but you could using conditional order by
insert into #t3
select code from #t1
--see the results
select * from #t3 order by id
drop table #t1, #t2, #t3
This returns the following, which has your order for the first table (1-7) but no guaranteed order for the second unless you apply some logic to specify the order of that table. As it is, there is no way to tell. A single clustered index could help.
+----+-----------+
| id | code |
+----+-----------+
| 1 | 102323223 |
| 2 | 105454545 |
| 3 | 106232244 |
| 4 | 106432432 |
| 5 | 106043222 |
| 6 | 206122222 |
| 7 | 211111116 |
| 8 | 106164029 |
| 9 | 106100797 |
| 10 | 106100793 |
| 11 | 106301098 |
| 12 | 106010735 |
| 13 | 206010989 |
| 14 | 206010956 |
+----+-----------+