2011-08-26 267 views
0

大家好,我必須解決一個問題:使用ssis加載excel電子表格,解釋數據,執行數字生成練習,然後插入到sql服務器數據庫。我可以閱讀excel電子表格,並將數據輸出正常。然而,我面臨的問題是數字生成部分,然後插入到數據庫中。SSIS讀取excel電子表格並生成編號並插入到db

我的Excel電子表格看起來是這樣的:

Range  Location 0 1 2 3 4 5 6 7 8 9     
01132 21 Leeds  Y  Y Y  Y Y  Y 

因此,例如,我們讀到利茲範圍內有一個「Y」的2列下,這意味着我們需要01132212000之間產生號碼 - 01132212999 。我有點確保我們可以讀取excel speradsheet,檢查一個數字是否在其下有'Y',爲這個範圍生成這些數字,然後將所有生成的數字插入到數據庫中。有任何想法嗎?

+0

你的例子不清楚。請詳細說明 – hungryMind

+0

我們是否還需要生成01132210000和01132210999之間的數字,01132213000和01132213999之間的數字等?我們是否生成這些範圍內的所有數字?它們是作爲整數還是作爲字符串存儲(我正在查看前導零)? –

+0

如果0中有'Y',我們需要生成01132210000和01132210999之間的數字。如果2中有'Y',則生成01132212000和0113222999之間的數字,依此類推。我們需要生成範圍內的所有數字,並將它們存儲爲nvarchar。第8位數字是表格中的數字,這決定了開始。所以例如2有一個'Y',所以我們需要2000到2999之間的數字 –

回答

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源」上生成行號...使用腳本來源:

  1. 添加一列,這只是一個DT_I4。
  2. 在CreateNewOutputRows中,使用for循環迭代1000次,在循環內使用Output0Buffer.AddRow,並將列設置爲循環值。

使用OLE DB源:

  1. 創建CTE或其他一些T-SQL魔術(這我沒有資格暗示 - 我只知道這是可能的),以創建一個「數字表「從1到1000,並從中選擇。
+0

有什麼機會可以讓我更詳細地介紹一下?我得到了第一點,但是從「那麼你需要一個源代碼 - 一個腳本源代碼或一個狡猾的OLE DB源代碼」開始有點失落! –

+0

希望修正幫助... –

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有點生疏,我沒有一個在我面前玩的例子,所以恐怕我不能幫你做正常情況。

相關問題