You can assign two ROW_NUMBER
s based on a random sort and join on them:
select
t.id,
g.id as gebruikerid
from
(
select id,
(row_number() over (order by newid())
% (select count(*) from gebruiker))+1 as rn -- number from 1 to n
from topic
) as t
join
( select id,
row_number() over (order by newid()) as rn -- sequential number from 1 to n
from gebruiker
) as g
on t.rn = g.rn
order by t.id
See fiddle
EDIT:
You can use the result of this SELECT to update the Topic
table:
update t
set gebruikerid = dt.gebruikerid
from topic as t
join
(
select
t.id,
g.id as gebruikerid
from
(
select id,
(row_number() over (order by newid()) % (select count(*) from gebruiker))+1 as rn
from topic
) as t
join
( select id,
row_number() over (order by newid()) as rn
from gebruiker
) as g
on t.rn = g.rn
) as dt
on t.id = dt.id;