大家好我必须解决使用ssis加载excel电子表格,解释数据,执行数字生成练习然后插入sql server数据库的问题。我可以阅读excel电子表格并获取数据。然而,我面临的问题是数字生成部分,然后插入数据库。
我的excel电子表格看起来像这样:
Range Location 0 1 2 3 4 5 6 7 8 9
01132 21 Leeds Y Y Y Y Y Y
因此,例如,我们读到利兹范围在2列下有一个'Y',这意味着我们需要生成01132212000 - 01132212999之间的数字。我有点确保我们如何阅读excel speradsheet,检查查看数字下面是否有“Y”,为该范围生成这些数字,然后将所有生成的数字插入数据库。有什么想法吗?
答案 0 :(得分:1)
实际上并不太难 - 如果我可以假设每个“数字”都应该显示为一行。
您需要一个带有Excel源的数据流(这将指示您运行your package in 32-bit mode)。然后,您可以使用Unpivot component将“数字”列转换为行,因此每个范围/位置都有一行带有Y或N.使用条件拆分过滤掉N行,您就是刚离开Ys。然后你需要一个源 - 一个Script source或一个狡猾的OLE DB源 - 来生成1000行,编号从0到999.你将在Excel行和数字行之间执行cartesian join using Derived Columns, Sorts, and Merge Join下一个。然后,您可以使用派生列生成您想要的“真实”数字,将其转换为字符串,并用零填充它。
澄清“脚本源或狡猾的OLE DB源”以生成行号...使用脚本作为源:
使用OLE DB源:
答案 1 :(得分:0)
就个人而言,我会在数据库中解决这个问题。在数据库中创建一个表来保存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有点生疏,我面前没有一个实例可以玩,所以我担心我无法帮助你处理正常情况。