就我個人而言,我會在數據庫中解決這個問題。在您的數據庫中創建一個表格,以保存您Excel表格中的原始數據。然後,您就可以解決問題這樣的SQL:
create table #excel (
range nvarchar(7),
location nvarchar(20),
col_0 nvarchar(1),
col_1 nvarchar(1),
col_2 nvarchar(1),
col_3 nvarchar(1),
col_4 nvarchar(1),
col_5 nvarchar(1),
col_6 nvarchar(1),
col_7 nvarchar(1),
col_8 nvarchar(1),
col_9 nvarchar(1)
)
/*Use SSIS to load your Excel sheet in, instead of this insert*/
insert into #excel
values ('0113221', 'Leeds', 'Y', NULL, 'Y', 'Y', NULL, 'Y', 'Y', NULL, 'Y', NULL)
;with numbers as
(
select 0 x
union all
select x + 1
from numbers
where x < 99
)
select e.location, e.range + '0' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_0 = 'Y'
union all
select e.location, e.range + '1' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_1 = 'Y'
union all
select e.location, e.range + '2' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_2 = 'Y'
union all
select e.location, e.range + '3' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_3 = 'Y'
union all
select e.location, e.range + '4' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_4 = 'Y'
union all
select e.location, e.range + '5' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_5 = 'Y'
union all
select e.location, e.range + '6' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_6 = 'Y'
union all
select e.location, e.range + '7' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_7 = 'Y'
union all
select e.location, e.range + '8' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_8 = 'Y'
union all
select e.location, e.range + '9' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_9 = 'Y'
如果你把它加載到下面的格式(或類似),您可以規範你的數據,你可以節省大量的凌亂代碼:
create table #excel (
range nvarchar(7),
location nvarchar(20),
number nvarchar(1),
yes_no nvarchar(1)
)
insert into #excel
values ('0113221', 'Leeds', '0', 'Y'),
('0113221', 'Leeds', '1', NULL),
('0113221', 'Leeds', '2', 'Y'),
('0113221', 'Leeds', '3', 'Y'),
('0113221', 'Leeds', '4', NULL),
('0113221', 'Leeds', '5', 'Y'),
('0113221', 'Leeds', '6', 'Y'),
('0113221', 'Leeds', '7', NULL),
('0113221', 'Leeds', '8', 'Y'),
('0113221', 'Leeds', '9', NULL)
;with numbers as
(
select 0 x
union all
select x + 1
from numbers
where x < 99
)
select e.location, e.range + e.number + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.yes_no = 'Y'
我的SSIS有點生疏,我沒有一個在我面前玩的例子,所以恐怕我不能幫你做正常情況。
你的例子不清楚。請詳細說明 – hungryMind
我們是否還需要生成01132210000和01132210999之間的數字,01132213000和01132213999之間的數字等?我們是否生成這些範圍內的所有數字?它們是作爲整數還是作爲字符串存儲(我正在查看前導零)? –
如果0中有'Y',我們需要生成01132210000和01132210999之間的數字。如果2中有'Y',則生成01132212000和0113222999之間的數字,依此類推。我們需要生成範圍內的所有數字,並將它們存儲爲nvarchar。第8位數字是表格中的數字,這決定了開始。所以例如2有一個'Y',所以我們需要2000到2999之間的數字 –