you can create a helper numbers table for this:
-- create helper numbers table, faster than online recursive CTE
-- can use master..spt_values, but actually numbers table will be useful
-- for other tasks too
create table numbers (n int primary key)
;with cte_numbers as (
select 1 as n
union all
select n + 1 from cte_numbers where n < 5000
)
insert into numbers
select n
from cte_numbers
option (maxrecursion 0);
and then insert some numbers you don't have in TableA (using join on row_number()
so you can insert multiple rows at once):
;with cte_n as (
select n.n, row_number() over(order by newid()) as rn
from numbers as n
where not exists (select * from tableA as t where t.columnA = n.n)
), cte_b as (
select
columnB, row_number() over(order by newid()) as rn
from tableB
)
insert into TableA(columnA, columnB)
select n.n, b.ColumnB
from cte_b as b
inner join cte_n as n on n.rn = b.rn
If you're sure that there could be only one row from TableB which will be inserted, you can
use this query
insert into TableA(columnA, columnB)
select
a.n, b.columnB
from tableB as b
outer apply (
select top 1 n.n
from numbers as n
where not exists (select * from tableA as t where t.columnA = n.n)
order by newid()
) as a
Note it's better to have index on ColumnA
column to check existence faster.
sql fiddle demo