我已经在表中创建了一个空列“ ZipCodes”。我想用以下数字自动填充此列:1000-39004000-9999
因此,所有这些序列号都必须在1列中,以1000开头,以9999结尾,但我不想包含范围3901-3999。如何在SQL中执行此操作?
这里是执行此操作的一种方法。这将生成1000到10000之间的数字,而忽略3901和4000之间的数字。
with row_generator
as (
select *
from master..spt_values
)
,ten_thousand_rows
as (
select top 10000
row_number() over(order by (select null))+1000 as rnk
from row_generator a
join row_generator b
on 1=1
)
select rnk
from ten_thousand_rows
where rnk between 1000 and 10000
and NOT(rnk between 3901 and 3999)
此后您可以做一个
with row_generator
as (
select *
from master..spt_values
)
,ten_thousand_rows
as (
select top 10000
row_number() over(order by (select null))+1000 as rnk
from row_generator a
join row_generator b
on 1=1
)
insert
into dest_table(autofill_column)
select rnk
from ten_thousand_rows
where rnk between 1000 and 10000
and NOT(rnk between 3901 and 3999)