【问题标题】:How to load data from excel to SQL DB using Azure Data Factory如何使用 Azure 数据工厂将数据从 Excel 加载到 SQL DB
【发布时间】:2021-09-21 11:39:12
【问题描述】:

我有一个 Excel 源。如何使用 Azure 数据工厂将数据从 excel 加载到 SQL DB?

【问题讨论】:

  • 是否可以预先修改 excel 文件输入?确实需要更多关于这方面的背景信息,因为有几种不同的方法可以做到这一点,但它们可能并不都适合你试图完成这项工作的确切方式。需要知道 excel 是否总是先输入 1.Name,然后输入 2.Class?更多类似的细节。
  • 是的,它会完全高于输入

标签: sql-server azure-sql-database azure-data-factory azure-data-factory-2


【解决方案1】:

有很多方法可以做到这一点,但我会告诉你我认为最简单的方法。

  1. 在 Azure 数据工厂中设置复制数据活动以复制数据 从 excel 到 Azure SQLDB 临时表。
  2. 在 Azure SQLDB 中创建一个存储过程,该过程将从暂存表插入到最终输出表中
  3. 将存储过程活动连接到 ADF 中的复制数据活动,以引用您刚刚在数据库中创建的过程,这样 ADF 将在加载登台表后立即运行存储过程。
  4. 您可以在加载最终输出表之后或在 ADF 中的复制活动之前使用命令/过程来清除暂存表。

请参阅下面的屏幕截图和 cmets 进行演练(在不使用 Azure 函数或其他东西的情况下,目前无法在 ADF 中以本机方式获取工作表名称,但我已尽我所能。如果 excel 是命名与可以使用的工作表相同,这是一个选项,我在这个例子中做了什么): 整体管道视图 第一步截断 Stage Table 以确保在加载之前它是空的 第二步加载带有文件输入的 Stage 表 最后一步是运行存储过程 这是我在下面制作的存储过程的代码,以及 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

如果这对您有用,请接受作为答案,如果您有任何其他 cmets 或问题,请告诉我。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 2020-06-12
    • 1970-01-01
    • 2019-08-05
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多