簡體   English   中英

如何使用 Azure 數據工廠將數據從 excel 加載到 SQL DB

[英]How to load data from excel to SQL DB using Azure Data Factory

我有一個 Excel 源。 如何使用 Azure 數據工廠將數據從 excel 加載到 SQL DB?

在此處輸入圖片說明

有很多方法可以做到這一點,但我將向您展示我認為最簡單的方法。

  1. 在 Azure 數據工廠中設置復制數據活動,以將數據從 excel 復制到 Azure SQLDB 臨時表中。
  2. 在 Azure SQLDB 中創建一個存儲過程,該過程將從暫存表插入到最終輸出表中
  3. 將存儲過程活動連接到 ADF 中的復制數據活動以引用您剛剛在數據庫中創建的過程,這樣 ADF 將在加載臨時表后立即運行存儲過程。
  4. 您可以使用命令/過程在加載最終輸出表之后或在 ADF 中的復制活動之前清除臨時表。

請參閱下面的屏幕截圖和評論以進行演練(在不使用 Azure 函數或其他東西的情況下,目前無法在 ADF 中以本機方式獲取工作表名稱,但我已盡可能多地覆蓋。如果 excel 的名稱相同作為可以替代的工作表,這是一個選項,我在這個例子中做了什么):整體管道視圖管道 第一步截斷舞台表以確保在加載之前它是空的截斷表 第 2 步加載帶有文件輸入的 Stage 表ADF 復制活動和數據輸入/輸出 最后一步是運行存儲過程下面是我在下面制作的存儲過程的代碼,加上 ADF 存儲過程活動的屏幕截圖:

create procedure stag.ttt_test (@ExcelFileName varchar(100))
as

DECLARE @ClassStartRow int

select @ClassStartRow = max(RowId)
from (
    select
    case when Details = '2.Class' then ROW_NUMBER() over (order by %%physloc%%) else 0 end as RowId
    from Stag.ttt_test_stage
    ) as sub
;

INSERT INTO Stag.ttt_test_final
select
    Case 
    when January is NOT NULL then DATEADD(day, -1, cast('2/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when February is NOT NULL then DATEADD(day, -1, cast('3/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when March is NOT NULL then DATEADD(day, -1, cast('4/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when April is NOT NULL then DATEADD(day, -1, cast('5/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when May is NOT NULL then DATEADD(day, -1, cast('6/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when June is NOT NULL then DATEADD(day, -1, cast('7/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when July is NOT NULL then DATEADD(day, -1, cast('8/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when August is NOT NULL then DATEADD(day, -1, cast('9/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when September is NOT NULL then DATEADD(day, -1, cast('10/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when October is NOT NULL then DATEADD(day, -1, cast('11/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when November is NOT NULL then DATEADD(day, -1, cast('12/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when December is NOT NULL then DATEADD(day, -1, cast('1/1/' + cast(YEAR(getdate()) + 1 as varchar(4)) as date))
    end as [Date],
    'name' as Category,
    Details as [Type],
    Coalesce(January, February, March, April, May, June, July, August, September, October, November, December) as [value],
    @ExcelFileName as SheetName
from (
    select
    ROW_NUMBER() over (order by %%physloc%%) as RowId,
    st.*
    from Stag.ttt_test_stage as st
    ) as sub
    where RowId > 1
    and RowId < @ClassStartRow

    ;

INSERT INTO stag.ttt_test_final
select
    Case 
    when January is NOT NULL then DATEADD(day, -1, cast('2/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when February is NOT NULL then DATEADD(day, -1, cast('3/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when March is NOT NULL then DATEADD(day, -1, cast('4/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when April is NOT NULL then DATEADD(day, -1, cast('5/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when May is NOT NULL then DATEADD(day, -1, cast('6/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when June is NOT NULL then DATEADD(day, -1, cast('7/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when July is NOT NULL then DATEADD(day, -1, cast('8/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when August is NOT NULL then DATEADD(day, -1, cast('9/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when September is NOT NULL then DATEADD(day, -1, cast('10/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when October is NOT NULL then DATEADD(day, -1, cast('11/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when November is NOT NULL then DATEADD(day, -1, cast('12/1/' + cast(YEAR(getdate()) as varchar(4)) as date))
    when December is NOT NULL then DATEADD(day, -1, cast('1/1/' + cast(YEAR(getdate()) + 1 as varchar(4)) as date))
    end as [Date],
    'class' as Category,
    Details as [Type],
    Coalesce(January, February, March, April, May, June, July, August, September, October, November, December) as [value],
    @ExcelFileName as SheetName
from (
    select
    ROW_NUMBER() over (order by %%physloc%%) as RowId,
    st.*
    from Stag.ttt_test_stage as st
    ) as sub
    where RowId > @ClassStartRow

存儲過程 ADF 活動

如果這對您有用,請接受作為答案,如果您有任何其他意見或問題,請告訴我。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM