How to create temp tables in SQL to be used in several ADF activities?

ε祈祈猫儿з 提交于 2020-01-24 19:47:05

问题


I need to create a global temp table in my SQL Server while executing an Azure Data Factory pipeline. This table will be used in several activities.

I already tried several approaches including one using the Stored Procedure activity targeting the sys.sp_executesql SP and the CREATE TABLE statement as the parameter. With this approach the table is actually created, but it's automaticaly dropped a second later, I don't understand why.

This is the script used to create the temp table:

CREATE TABLE ##tempGL
(
    GLAccount NVARCHAR(15),
    GLSubAccount NVARCHAR(15)
)

So, how can I create a SQL Server temp table from an Azure Data Factory Pipeline activity that persists until I dropped it?


回答1:


I have been struggling with this myself. Apparently this is by design (see quote below from Microsoft employee) and it is not possible to achieve this using Azure Data Factory even though the documentation mentions that it is possible.

That is by design. We won’t keep connection between 2 activities. If you use a real table instead of temporary table. Then you will get the expected result. The suggestion is don’t used temporary table in ADF if the data need more than 1 activities to access.

https://github.com/MicrosoftDocs/azure-docs/issues/35449#issuecomment-517451867

The reason this happens is the session is dropped when a pipeline activity ends, which causes the temporary table to also be dropped.

Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them. The association between a task and a table is maintained only for the life of a single Transact-SQL statement. This means that a global temporary table is dropped at the completion of the last Transact-SQL statement that was actively referencing the table when the creating session ended.

https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-2017#temporary-tables

Hopefully Microsoft fixes this at some point and makes it possible to use temporary tables across activities with Azure Data Factory.


I have raised this as a suggestion for Azure here https://feedback.azure.com/forums/270578-data-factory/suggestions/38287108-persist-global-temporary-tables-between-activities

For anyone reading this that might want his feature please upvote that suggestion.



来源:https://stackoverflow.com/questions/56119601/how-to-create-temp-tables-in-sql-to-be-used-in-several-adf-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!