我可以在1到20之间选择该表中不存在的数字,然后将我的一个数字插入到sql列中吗?
example i have in my table 1
2
9
13
if not exists select ('%[1-20]%',column from table); insert into table select....?
答案 0 :(得分:0)
使用数字table ..
;With cte
as
(
select top 20 number
from numbers
order by number
)
Insert into yourtable
select top 1* from cte c1 where not exists(select 1 from yourtable t2 where t1.number=t2.id)
您也可以使用加入..
Insert into yourtable
select top 1 number
from
numbers n
join
yourtable t
on t.id<>n.number
and n<=20