Synapse dynamic copy command with wild card

Moniker131 25 Reputation points
2023-11-15T15:08:24.0133333+00:00

Having trouble using dynamic sql with the COPY command to import multiple files into a table. I have multiple environments, so the blob path is parameterized, and looks something like this:

'https://' + @storageaccountname +'.endpoint/container/folder/*.csv'

When I try to execute a copy command dynamically like this, it interprets /* as beginning a comment block. I cannot find a way to escape this comment block.

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,373 questions
{count} votes

Accepted answer
  1. Smaran Thoomu 24,110 Reputation points Microsoft External Staff Moderator
    2023-11-21T11:43:16.46+00:00

    Moniker131 To escape the /* in your dynamic SQL query, you can use the CHAR function to insert the ASCII code for the forward slash and asterisk characters. Here's an example:

    SET @path = 'https://' + @storageaccountname + '.endpoint/container/folder/' + CHAR(47) + CHAR(42) + '.csv'
    SET @sql = 'COPY INTO myTable FROM ''' + @path + ''' WITH (FORMAT=''CSV'')'
    EXEC (@sql)
    

    In this example, CHAR(47) inserts the ASCII code for the forward slash character, and CHAR(42) inserts the ASCII code for the asterisk character. This will allow you to use the wildcard in your path without it being interpreted as a comment block.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-11-16T08:45:23.5633333+00:00

    In Azure Synapse Analytics, when using the COPY command for dynamic SQL with wildcards, you may encounter an issue where the wildcard character is misinterpreted. To correctly use wildcards in the file path for the COPY command, you should be aware of the following:

    1. Wildcard path name matching is case-sensitive.
    2. Wildcards can be escaped using the backslash character ().
    3. Wildcard expansion is applied recursively, meaning all files matching the pattern in the specified directory and its subdirectories are included.

    For example, to load all CSV files under a specified folder (including subdirectories), you can use a path like Account/Container/Folder/*.csv. In your case, ensure the wildcard is correctly formatted and escaped if necessary​​.

    https://learn.microsoft.com/en-us/sql/t-sql/statements/copy-into-transact-sql?view=azure-sqldw-latest


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.