I have two tables, `TableA` and `TableB` like this:

```
TableA
-------------------------------------------
|  id  |  some_data  | new_FK_column_on_B |
| ---- | ----------- | ------------------ |
|   1  |     ...     |        null        |
|  ... |     ...     |        null        |
|  999 |     ...     |        null        |
-------------------------------------------

TableB
----------------------------
|  id  |  some_other_data  |
| ---- | ----------------- |
|      |                   |
----------------------------

```

At the moment, `TableB` is empty, and FK column in `TableA` is `null` for all rows. I need to write one-time initializing scrit to populate `TableB` and initialize FK column for some rows (criterial, not for all) in `TableA` by identifiers from rows, inserted in `TableB`.

I know two ways to do this:

1) using `while` and `scope_identity()`, inserting new row into `TableB` and updating `TableA` on each iteration, while exists rows in `TableA`, which should be updated

```
while (exists (select 1 from TableA where [condition]))
begin
    insert into TableB (some_other_data) values ('some_other_data')

    update TableA set new_FK_column_on_B
    where id = (select top 1 id from TableA where [condition])
end

```

2) create temp column in `TableB`, storing `id` of row in `TableA`, for which it was inserted, and then update `TableA` using `join`

```
alter table TableB add temp int
go

insert into TableB (some_other_data, temp) select 'some_other_data', id from TableA where [condition]

update TableA
set new_FK_column_on_B = b.id
from TableB as b
join TableA as a on a.id = b.temp

alter table TableB drop column temp

```

Also I was trying to use somehow `output` from `insert` like this, but it's syntax is incorrect:

```
update TableA
set new_FK_column_on_B =
(
    select insertedId from 
    (
        insert into TableB (some_other_data)
        output inserter.id as insertedId
        values ('some_other_data')
    )
)
where [condition]

```

Is there any easier way to do this whithout using `while` or modifing any table?