Technical Article

Create Dynamic External Table in Azure synapse Analytics

,

Creating a dynamic external table in Azure Synapse Analytics allows you to ingest and process data from various external data sources without the need to copy or move the data into your data warehouse. By using dynamic external tables, you can access data residing in various storage systems, such as Azure Data Lake Storage, Azure Blob Storage, or SQL Server, and query it directly within your Synapse Analytics workspace.

 

 

CREATE PROCEDURE [dbo].[copy_to_extrnl_tbl] 
@p_query VARCHAR(MAX), 
@p_schma_nm [VARCHAR](255), 
@p_extrnl_tbl_nm [VARCHAR](255), 
@p_loctn VARCHAR(1024), 
@p_data_src [VARCHAR](255), 
@p_file_frmt [VARCHAR](255), 
@p_debug_mode [BIT] 
AS
BEGIN

DECLARE @v_sql_text NVARCHAR(4000) = '
IF EXISTS (SELECT 1 FROM sys.external_tables WHERE object_id = OBJECT_ID(''[' + @p_schma_nm + '].[' + @p_extrnl_tbl_nm + ']''))
DROP EXTERNAL TABLE [' + @p_schma_nm + '].[' + @p_extrnl_tbl_nm + '];' + '
CREATE EXTERNAL TABLE [' + @p_schma_nm + '].[' + @p_extrnl_tbl_nm + ']
    WITH
    (
LOCATION = '''+ @p_loctn + ''',
        DATA_SOURCE = [' + @p_data_src + '],
        FILE_FORMAT = [' + @p_file_frmt + ']
    )  AS ' + @p_query ;

   /*======================================================================
   Execute the dynamic SQL that was generated
   ======================================================================*/   
    IF @p_debug_mode = 1
        SELECT @v_sql_text;
    ELSE
        EXECUTE sp_executesql @v_sql_text;

END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating